Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Python print Unicode symbols? [duplicate]

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.

like image 441
Euphorbium Avatar asked Jan 22 '13 10:01

Euphorbium


People also ask

How do you print Unicode symbols in Python?

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.

Can Python handle Unicode?

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.

How do you stop Unicode errors in Python?

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.

What does Unicode () do in Python?

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.


1 Answers

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.

like image 167
Martijn Pieters Avatar answered Oct 18 '22 03:10

Martijn Pieters