这个问题的标题与
this one
,但我的问题不同。
我使用tarfile.tarfile.textractfile()从tar.gz档案中读取了数据,并将其存储在xml.etree.ElementTree.Element中。数据是utf-8编码的。
当我尝试使用xml.etree.ElementTree.ElementTree.write()将其放入文件中时,我得到的是:
Traceback (most recent call last):
File "../python/GetDoc.py", line 150, in <module>
ET.ElementTree(elements).write(args.outfile, encoding="us-ascii", method="text")
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 815, in write
_serialize_text(write, self._root, encoding)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1007, in _serialize_text
write(part.encode(encoding))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)