简体   繁体   English

python'ascii'编解码器无法编码字符

[英]python 'ascii' codec can't encode character

I keep getting an error when trying to display content from an rss feed. 尝试显示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“厨房接收器”提要(utf-8)和AMD新闻提要(未设置编码),它们均已下载到我的计算机上,因此每次运行代码时我都不会ping它们的服务器。

teksyndicate feed gives me 'UnicodeEncodeError: 'charmap' codec can't encode character u'\\xc2' in position 183: character maps to ' teksyndicate feed给我'UnicodeEncodeError:'charmap'编解码器无法对位置183处的字符u'\\ xc2'进行编码:字符映射为'

amd feed gives me 'UnicodeEncodeError: 'charmap' codec can't encode character u'\™' in position 349: character maps to ' the code throwing the error: amd feed给我'UnicodeEncodeError:'charmap'编解码器无法对位置349中的字符u'\\ u2122'进行编码:字符映射为'引发错误的代码:

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. 只是一个额外的说明,我正在尝试制作一个RSS供稿阅读器。 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. 在这一点上,我只是想尝试学习ElementTree。

The print statement tries to represent everything as strings. print语句尝试将所有内容表示为字符串。 You question essentially duplicates this one here . 您所质疑的基本上是在此重复这一步骤 I found it with Google, as should you have! 我在Google上找到了它,你也应该找到它!

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). 问题在于,除非您指定并使用ASCII编码,否则许多Unicode字符将无法转换为ASCII(其中只有128个字符)。 The answers given to the other question should tell you what to correct. 另一个问题的答案应该告诉您要纠正的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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