Python中的口音

时间:2014-05-20 16:51:04

标签: python unicode utf-8

我知道这个问题并不新鲜,但我不知道如何解决它。我需要将.opl文件(vim大纲格式)转换为.mm(自由思想)。重点是原始文件是用意大利语编写的,所以充满了重音,在使用python脚本(plugin)进行转换时,我收到了一个unicode错误。这是细节

python outline_freemind.py test.otl > test.mm
File "outline_freemind.py", line 114, in <module>
    xmltree.write(sys.stdout, 'utf-8')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 815, in write
    serialize(write, self._root, encoding, qnames, namespaces)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 934, in _serialize_xml
    _serialize_xml(write, e, encoding, qnames, None)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 934, in _serialize_xml
    _serialize_xml(write, e, encoding, qnames, None)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 934, in _serialize_xml
    _serialize_xml(write, e, encoding, qnames, None)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 927, in _serialize_xml
    v = _escape_attrib(v, encoding)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1085, in _escape_attrib
    return text.encode(encoding, "xmlcharrefreplace")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 9: ordinal not in range(128)

我试图在脚本的开头插入

# -*- coding:utf-8-*-

没有成功。

1 个答案:

答案 0 :(得分:1)

插件的声明编码与您遇到的问题无关:声明的唯一目的是告诉Python解释器程序本身的编码,并且您的程序不包含在ASCII范围之外的字符,所以不可能曲解。

如果问题与程序的编码有关,则会出现如下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "outline_freemind.py", line 115
SyntaxError: Non-ASCII character '\xc3' in file outline_freemind.py on line 115, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

...但是因为你实际上得到的是UnicodeDecodeError,这意味着错误在你的插件处理非ASCII输入中...所以你应该向插件的作者报告。< / p>