简体   繁体   中英

python 'ascii' codec can't encode character

I keep getting an error when trying to display content from an rss feed. the feeds I have tried are the Teksyndicate "kitchen sink" feed(utf-8) and the AMD news feed(encoding not set), both downloaded to my computer so I do not ping their servers evry time i run the code.

teksyndicate feed gives me 'UnicodeEncodeError: 'charmap' codec can't encode character u'\\xc2' in position 183: character maps to '

amd feed gives me 'UnicodeEncodeError: 'charmap' codec can't encode character u'\™' in position 349: character maps to ' the code throwing the error:

import xml.etree.ElementTree as ET
xmlTree = ET.parse('amd.rss')
xmlRoot = xmlTree.getroot()
# <tag attrib>text<child/>...</tag>tail
# above pulled from Element tree lib file
for i in list(xmlTree.iter()):
    if i.text != None:
        print i.tag + ': ' + i.text
    else:
        print i.tag + ': None'
print '\n\nxmlRoot'
print xmlRoot.tag
print xmlRoot.attrib
print xmlRoot.text
print xmlRoot.tail

Just a additional note, I am trying to make an rss feed reader. I know there are ones out there, but I want to make my own just to give it a shot. That is when I ran into this error, and I have no Idea how to fix it. At this point I'm just goofing off trying to learn ElementTree.

The print statement tries to represent everything as strings. You question essentially duplicates this one here . I found it with Google, as should you have!

The problem is that unless you specify and encoding ASCII will be used, and many Unicode characters can't be converted to ASCII (which only has 128 characters in it). The answers given to the other question should tell you what to correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM