Python 2.7 统一码解码错误 : 'ascii' codec can't decode byte

标签 python unicode

我一直在解析一些带有特殊字符(捷克字母)的 docx 文件(UTF-8 编码的 XML)。当我尝试输出到 stdout 时,一切顺利,但我无法将数据输出到文件,

Traceback (most recent call last):
File "./test.py", line 360, in
ofile.write(u'\t\t\t\t\t\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 37: ordinal not in range(128)

尽管我明确地将 word 变量转换为 unicode 类型(type(word) 返回了 unicode),但我尝试使用 .encode('utf -8) 我仍然被这个错误困住了。

这是现在的代码示例:

for word in word_list:
    word = unicode(word)
    #...
    ofile.write(u'\t\t\t\t\t<feat att="writtenForm" val="'+word+u'"/>\n')
    #...

我还尝试了以下方法:

for word in word_list:
    word = word.encode('utf-8')
    #...
    ofile.write(u'\t\t\t\t\t<feat att="writtenForm" val="'+word+u'"/>\n')
    #...

甚至这两者的结合:

word = unicode(word)
word = word.encode('utf-8')

我有点绝望,所以我什至尝试在 ofile.write()

中对单词变量进行编码
ofile.write(u'\t\t\t\t\t<feat att="writtenForm" val="'+word.encode('utf-8')+u'"/>\n')

对于我做错了什么的任何提示,我将不胜感激。

最佳答案

ofile 是一个字节流,您正在向其中写入字符串。因此,它会尝试通过编码为字节字符串来处理您的错误。这通常只对 ASCII 字符安全。由于 word 包含非 ASCII 字符,因此失败:

>>> open('/dev/null', 'wb').write(u'ä')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0:
                    ordinal not in range(128)

通过使用 io.open 打开文件,使 ofile 成为文本流,具有类似 'wt' 的模式和显式编码:

>>> import io
>>> io.open('/dev/null', 'wt', encoding='utf-8').write(u'ä')
1L

或者,您也可以使用 codecs.open使用几乎相同的界面,或使用 encode 手动编码所有字符串.

关于Python 2.7 统一码解码错误 : 'ascii' codec can't decode byte,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13512443/

相关文章:

python - UnboundLocalError : local variable 'batch_outputs' referenced before assignment

python - pygame - 杀死组中的 Sprite 的问题

c++ - Linux Ubuntu 中的文件内输出 unicode 符号

python - 在 python 解释器中打印 unicode 字符

python - 在 Pandas 列中应用拆分并获取结果的第二个元素,该列有时包含 None 并且有时不会拆分为超过 1 个组件

python - factory_boy : add several dependent objects

python - 接受来自 docopt 的任意选项

unicode - 简化字符串、删除变音符号的通用方法

html - 如何添加相当于 unicode 字符的 "alt"属性(在一个范围内)?

java - 使用正则表达式搜索 unicode 文本