Why can't Python print Unicode characters?

Possible duplicate:
Python UnicodeDecodeError - Am I misunderstanding the encoding?

I am having trouble printing some Unicode characters in Python as follows:

# encoding: utf-8 print u'ฤ™ฤ—ฤฏลณลกฤฏลกลซ' 

When I try to run this on my Ubuntu 12 VPS server with Python 2.7, I get an error:

UnicodeEncodeError: ascii codec cannot encode characters at position 0-7: serial number not in range (128)

Why is he trying to encode them in ASCII?

Commands execute correctly on my local machines.

The file is correctly encoded in utf-8.

+7
source share
1 answer

Printing unicode objects requires Python to guess the output encoding and encoding of Unicode code pages for that encoding.

On your VPS server, the output encoding looks like ASCII, which is the default when no encoding can be detected (for example, when using a channel). If you run the same code on the terminal, the final encoding is usually detected and the encoding ends.

The solution should be explicitly encoded depending on your script requirements.

Read the Python Unicode HOWTO to understand how Python performs this discovery and why it should be encoded for you.

+9
source

All Articles