views:

81

answers:

1

Hi,

I'm using windows and linux machines for the same project. The default encoding for stdin on windows is cp1252 and on linux is utf-8.

I would like to change everything to uft-8. Is it possible? How can I do it?

Thanks Eduardo

+1  A: 

You can do this by not relying on the implicit encoding when printing things. Not relying on that is a good idea in any case -- the implicit encoding is only used when printing to stdout and when stdout is connected to a terminal.

A better approach is to use unicode everywhere, and use codecs.open or codecs.getwriter everywhere. You wrap sys.stdout in an object that automatically encodes your unicode strings into UTF-8 using, for example:

sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

This will only work if you use unicode everywhere, though. So, use unicode everywhere. Really, everywhere.

Thomas Wouters
What about stdin?
duduklein
stdin isn't decoded automatically, so you always have to do this yourself. And assuming the input is UTF-8 is probably a bad idea, but there's `codecs.getreader('utf-8')(sys.stdin)` if you really want to.
Thomas Wouters