nam
nam

Reputation: 3632

Strange behavior of pydev with unicode

I'm trying to fix a bug in a python project. When the client enter some utf8 characters like 'é' into a program written with PyQt, there is an error . The client execute the program in console mode (activate the virtualenv and execute the script main.py) But when I run the same program under Eclipse and Pydev, I can not reproduce the problem (everything is ok). Also I'm sure that Eclipse use the same virtualenv as the client. How could it be possible that running the program inside Eclipse could be different than running in a command line? Thanks,

Upvotes: 1

Views: 375

Answers (1)

Radio-
Radio-

Reputation: 3171

Based on your comment, there are a few things going on. First, toPlainText already returns a unicode object. So the problem is here:

>>> str("é")
'\x82'
>>> unicode(str("é"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 0: ordinal
not in range(128)

According to http://docs.python.org/2/howto/unicode.html, "if you leave off the encoding argument, the ASCII encoding is used for the conversion, so characters greater than 127 will be treated as errors".

So if you still need to encode, then add an encoding1 argument, like:

>>> unicode(str("é"), 'latin_1')
u'\x82'

Upvotes: 1

Related Questions