小编Dan*_*ver的帖子

在Python中设置sax解析器的编码

当我将utf-8编码的xml提供给ExpatParser实例时:

def test(filename):
    parser = xml.sax.make_parser()
    with codecs.open(filename, 'r', encoding='utf-8') as f:
        for line in f:
            parser.feed(line)
Run Code Online (Sandbox Code Playgroud)

......我得到以下内容:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 72, in search_test
    parser.feed(line)
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/xml/sax/expatreader.py", line 207, in feed
    self._parser.Parse(data, isFinal)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb4' in position 29: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我可能在这里遗漏了一些明显的东西.如何将解析器的编码从"ascii"更改为"utf-8"?

python unicode sax

6
推荐指数
3
解决办法
7017
查看次数

标签 统计

python ×1

sax ×1

unicode ×1