By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

Encodings and printing unicode

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Fuzzyman wrote:
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
Jul 18 '05 #2

P: n/a

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


sys.stdout.encoding
[snip..]


Aha... that's the missing piece of information. Thank you.

Regards,

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

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.