我猜这个错误来自XML文件是UTF-8的事实,但是Python想把它作为ASCII来处理?我已经正确解析了xml,但是当我尝试打印XML的内容时,我得到了这个错误,因为有一些外来的Unicode字符。有人可以尝试修复下面的代码吗?可以将unicode字符串编码为ASCII吗?谢谢!
import xml.etree.ElementTree as ET
import sys
if __name__ == '__main__':
tree = ET.parse('/Downloads/sample.xml')
root = tree.getroot()
a=[]
for child in root.iter():
if child.tag == "author":
a.append(child.text)
if child.tag == "title":
for name in a:
print "%s\t%s" % (name, child.text)
a=[]