[英]UnicodeEncodeError: 'ascii' codec can't encode character […]
I have read the HOWTO on Unicode from the official docs and a full, very detailed article as well. 我从官方文档中读到了关于Unicode的HOWTO以及一篇完整,非常详细的文章 。 Still I don't get it why it throws me this error. 我仍然不明白为什么它会抛出这个错误。
Here is what I attempt: I open an XML file that contains chars out of ASCII range (but inside allowed XML range). 这是我尝试的内容:我打开一个包含ASCII范围内的字符的XML文件(但在允许的XML范围内)。 I do that with cfg = codecs.open(filename, encoding='utf-8, mode='r')
which runs fine. 我用cfg = codecs.open(filename, encoding='utf-8, mode='r')
运行正常。 Looking at the string with repr()
also shows me a unicode string. 用repr()
查看字符串也会显示一个unicode字符串。
Now I go ahead and read that with parseString(cfg.read().encode('utf-8')
. Of course, my XML file starts with this: <?xml version="1.0" encoding="utf-8"?>
. Although I suppose it is not relevant, I also defined utf-8 for my python script, but since I am not writing unicode characters directly in it, this should not apply here. Same for the following line: from __future__ import unicode_literals
which also is right at the beginning. 现在我继续用parseString(cfg.read().encode('utf-8')
读取它。当然,我的XML文件以这样开头: <?xml version="1.0" encoding="utf-8"?>
。虽然我想这是不相关的,我还定义了UTF-8我的Python脚本,但因为我不是在它直接写的Unicode字符,这不应该适用于此处同为以下行: from __future__ import unicode_literals
这也是一开始。
Next thing I pass the generated Object to my own class where I read tags into variables like this: xmldata.getElementsByTagName(tagName)[0].firstChild.data
and assign it to a variable in my class. 接下来我将生成的Object传递给我自己的类,在那里我将标签读入如下变量: xmldata.getElementsByTagName(tagName)[0].firstChild.data
并将其分配给我的类中的变量。
Now what perfectly works are those commands (obj is an instance of the class): 现在最完美的是那些命令(obj是类的一个实例):
for element in obj:
print element
And this command does work as well: 这个命令也可以工作:
print obj.__repr__()
I defined __iter__()
to just yield every variable while __repr__()
uses the typical printf stuff: "%s" % self.varname
我定义了__iter__()
来生成每个变量,而__repr__()
使用典型的printf东西: "%s" % self.varname
Both commands print perfectly and can output the unicode character. 两个命令都打印完美,可以输出unicode字符。 What does not work is this: 什么不起作用是这样的:
print obj
And now I am stuck because this throws the dreaded 现在我被卡住了,因为这会引起可怕的
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 47:
So what am I missing? 那我错过了什么? What am I doing wrong? 我究竟做错了什么? I am looking for a general solution, I always want to handle strings as unicode, just to avoid any possible errors and write a compatible program. 我正在寻找一个通用的解决方案,我总是希望将字符串作为unicode处理,只是为了避免任何可能的错误并编写兼容的程序。
Edit: I also defined this: 编辑:我也定义了这个:
def __str__(self):
return self.__repr__()
def __unicode__(self):
return self.__repr__()
From documentation I got that this 从文档我得到了这个
I finally solved it. 我终于解决了。 The problem was (I am not sure why) that if you called either __str__()
or __repr__()
directly it would be hapyp to handle it well, but printing it directly (as in: print obj
) does not work (although it should only just call __str__()
itself). 问题是(我不知道为什么),如果你直接调用__str__()
或__repr__()
,那么它将很好地处理它,但是直接打印它(如: print obj
)是行不通的(虽然它应该是只是调用__str__()
本身)。
The final help came from this article . 最后的帮助来自这篇文章 。 I already got to the step where I got it to print to the console (but a wrong letter) when I used utf-8 encoding. 当我使用utf-8编码时,我已经到了打印到控制台(但是错误的字母)的步骤。 Finally solved it to be perfectly correct by defining this: 最后通过定义它来解决它是完全正确的:
def __str__(self):
return self.__repr__().encode(stdout.encoding)
Now the only open question that remains is: Why do print obj.__str__()
and print obj
differently with this? 现在唯一尚未解决的问题是:为什么print obj.__str__()
并以不同方式print obj
? It does make no sense to me. 这对我来说毫无意义。 And yes, to stress that again: Calling the former or __repr__()
DID work. 是的,再次强调:调用前者或__repr__()
DID工作。 And still does with the explicit encoding. 并且仍然使用显式编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.