Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python program works in Eclipse but not when I run it directly (Unicode stuff)

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?

like image 437
Prince Merluza Avatar asked Mar 08 '26 02:03

Prince Merluza


2 Answers

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.

like image 85
Martijn Pieters Avatar answered Mar 09 '26 17:03

Martijn Pieters


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

like image 37
Qiau Avatar answered Mar 09 '26 17:03

Qiau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!