Possible Duplicate:
Python UnicodeDecodeError - Am I misunderstanding encode?
I am having trouble printing some unicode symbols in Python like this:
# encoding: utf-8
print u'ęėįųšįšū'
When I try to run this on my VPS Ubuntu 12 server with Python 2.7, I get an error:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)
Why does it try to encode them in ASCII?
The commands run correctly on my local machines.
The file is correctly encoded in utf-8.
Use the "\u" escape sequence to print Unicode characters In a string, place "\u" before four hexadecimal digits that represent a Unicode code point. Use print() to print the string.
Python's string type uses the Unicode Standard for representing characters, which lets Python programs work with all these different possible characters. Unicode (https://www.unicode.org/) is a specification that aims to list every character used by human languages and give each character its own unique code.
Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode( utf-8 ) and decode( utf-8 ) functions accordingly in your code.
If encoding and/or errors are given, unicode() will decode the object which can either be an 8-bit string or a character buffer using the codec for encoding. The encoding parameter is a string giving the name of an encoding; if the encoding is not known, LookupError is raised.
Printing unicode
objects requires Python to guess the output encoding and encoding the Unicode codepoints to that encoding.
On your VPS server, the output encoding appears to be ASCII, which is the default when no encoding could be detected (such as when using a pipe). If you run the same code on a terminal, the terminal encoding is usually detected and the encoding succeeds.
The solution is to encode explicitly depending on your script requirements.
Please do read the Python Unicode HOWTO to understand how Python does this detection and why it needs to encode for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With