3

I have searched and found some related problems but the way they deal with Unicode is different, so I can't apply the solutions to my problem.

I won't paste my whole code but I'm sure this isolated example code replicates the error: (I'm also using wx for GUI so this is like inside a class)

#coding: utf-8
...
something = u'ЧЕТЫРЕ'
//show the Russian text in a Label on the GUI
self.ExampleLabel.SetValue(str(self.something))

On Eclipse everything works perfectly and it displays the Russian characters. However when I try to open up Python straight through the file I get this error on the CL:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: 
ordinal not in range(128)

I figured this has something to do with the CL not being able to ouput the Unicode chars and Eclipse doing behind-the-scene magic. Any help on how to make it so that it works on its own?

4

3 回答 3

5

When you call str() on something without specifying an encoding, the default encoding is used, which depends on the environment your program is running in. In Eclipse, that's different from the command line.

Don't rely on the default encoding, instead specify it explicitly:

self.ExampleLabel.SetValue(self.something.encode('utf-8'))

You may want to study the Python Unicode HOWTO to understand what encoding and str() do with unicode objects. The wxPython project has a page on Unicode usage as well.

于 2012-09-02T09:11:16.090 回答
1

Try self.something.encode('utf-8') instead.

于 2012-09-02T09:10:50.917 回答
1

如果您使用 repr 而不是 str 它应该为您处理转换,并且还涵盖对象并不总是字符串类型的情况,但您可能会发现它在您的上下文中为您提供了一组额外的引号甚至 unicode u . repr 比 str 更安全 - str 假定为 ascii 编码,但 repr 将以与您在代码中看到它们的方式相同的方式显示您的代码点,因为使用 eval 包装应该将其转换回原来的样子 - repr 必须采用 python 代码的形式,即 ascii 安全,因为大多数 python 代码都是用 ascii 编写的。

于 2012-09-03T08:42:09.113 回答