UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 7: ordinal not in range(128)



I guess this error is coming from the fact that the XML file is in UTF-8, but Python wants to handle it as an ASCII? I have parsed the xml correctly but when I try to print the contents of the XML I get this error because there are some foreign Unicode characters. Can someone try fix this code below? May be encode unicode string to ASCII? Thanks!!



1.import xml.etree.ElementTree as ET

2.import sys

3. if __name__ == '__main__':
4. tree = ET.parse('/Downloads/sample.xml')
5. root = tree.getroot()
6. a=[]

7. for child in root.iter():
8. if child.tag == "author":
9. a.append(child.text)
10. if child.tag == "title":
11. for name in a:
12. print "%s\t%s" % (name, child.text)
13. a=[]

No comments:

Post a Comment