Encodings and printing unicode

F

Fuzzyman

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
 
K

Kent Johnson

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

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.

This uses sys.defaultencoding

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top