I’ve got following problem. If I’ll run my app in eclipse it works OK, but when I’ll run it in standalone debuger – I got following error:
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u0144′ in position 7: ordinal not in range(128)
How can I fix it?
My code fragment:
x = x.replace("Ł", "L")
Guess, based on (insufficient) information provided:
You are running Python 2.x.
[Guess]
xis astrobject.[Guess] Eclipse sets the default encoding to UTF-8.
The “standard debugger” sets the default encoding to ascii.
Result: splat.
Solution (standard operating procedure for working with Unicode):
strobjects to `unicode’.unicodeobjects using whatever encoding theconsumer of the output is expecting.
Important update Actually if
xwas a UTF-8-encodedstrobject, you should have got a message likeUnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 7: etc etc.Note that your actual error message says
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0144' in position 7: etc etcThis indicates that whatever it is complaining about is (a) aunicodeobject (b) at least 8 characters long. However you are saying in effect thatxis not aunicodeobject (otherwise x.decode(‘utf8’) would fail) and the other two args ofreplaceare only 1 character long. Consequently we have an impossibility.To help resolve this:
and edit your question to show the actual code that you ran plus the full error message and the traceback.
By the way: u’\u0144′ is LATIN SMALL LETTER N WITH ACUTE; does that info help at all?