Encodings and printing unicode

Fuzzyman fuzzyman at gmail.com
Mon Mar 14 07:18:46 EST 2005


How does the print statement decode unicode strings itis passed ? (By
that I mean which encoding does it use).

Under windows it doesn't appear to use defaultencoding. On my system
the default encoding is ascii, yet the terminal encoding is latin1 (or
cp1252 or whatever, but not ascii). This means that print '£' works
fine, yet unicode('£') will raise the UnicodeDecodeError.

However print u'£' will also work fine. (Under pythonce this same
statement raises a UnicodeDecodeError - but the 'terminal' is
different).

In my understanding unicode is an 'internal representation' - if you
want to write a unicode string to a file you have to turn it into a
byte string and specify an encoding (unless you pickle it - which is
cheating). So when you 'print' a unicode string, what is sent to
sys.stdout ? Is a character encoding used ? If so which one... if
not... why not ?

(In this case if defaultencoding was used it *ought* to raise a
UnicodeDecodeError).

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml




More information about the Python-list mailing list