Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用ElemTree使用UTF-8编码和bytestring解析XML文件_Python_Xml_Utf 8 - Fatal编程技术网

Python 使用ElemTree使用UTF-8编码和bytestring解析XML文件

Python 使用ElemTree使用UTF-8编码和bytestring解析XML文件,python,xml,utf-8,Python,Xml,Utf 8,我有以下完整的XML文件(实际文件可下载): 但是,它给了我以下错误消息: 24560200 Traceback (most recent call last): File "./parse_xml.py", line 41, in <module> parse_xml(fvar) File "./parse_xml.py", line 29, in parse_xml print pmid, title UnicodeEncodeError: 'ascii'

我有以下完整的XML文件(实际文件可下载):

但是,它给了我以下错误消息:

24560200 Traceback (most recent call last):
  File "./parse_xml.py", line 41, in <module>
    parse_xml(fvar)
  File "./parse_xml.py", line 29, in parse_xml
    print pmid, title
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 5: ordinal not in range(128)
24560200回溯(最近一次呼叫最后一次):
文件“/parse_xml.py”,第41行,在
解析xml(fvar)
文件“/parse_xml.py”,parse_xml中的第29行
打印pmid,标题
UnicodeEncodeError:“ascii”编解码器无法对位置5中的字符u'\xe8'进行编码:序号不在范围内(128)
解析和打印它的正确方法是什么?

已经回答:试试看

而不是

    print pmid, title

失败的是印刷。您在这里使用的是什么控制台或终端?@MartijnPieters:Mac Maverick默认终端。您是否将输出重定向到其他地方?Python无法读取终端编码。如果要重定向输出,则可以设置
pythonionecoding=UTF-8
。是的,通过标准unix
管道。如果某个问题已在其他地方得到回答,请投票以重复方式关闭该问题。@MartijnPieters:谢谢,我想我没有足够的重复次数。如果你还没有投票权直接关闭,你可以标记一个帖子。标记会将帖子放入关闭评论队列。@MartijnPieters:我明白了,谢谢。
24560200 Traceback (most recent call last):
  File "./parse_xml.py", line 41, in <module>
    parse_xml(fvar)
  File "./parse_xml.py", line 29, in parse_xml
    print pmid, title
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 5: ordinal not in range(128)
    print pmid.encode('utf8'), title.encode('utf8')
    print pmid, title