Filippo Valsorda
Filippo Valsorda

Reputation: 988

What should I do when sys.stdout.encoding is None?

I am trying to handle the different encodings in a Python script the more user-friendly and auto-magic way possible (there are APIs for utf8). It is a cross-platform console script.

For printing to stdout I use sys.stdout.encoding and it seems to do the right thing almost always when printing to the console. However when stdout is piped it becomes None.

So in that case I assume I am piping to a file and use locale.getpreferredencoding() but:

Upvotes: 2

Views: 861

Answers (1)

jsbueno
jsbueno

Reputation: 110156

If it is None, simply output your data as utf-8, and document it. If it happens that there are use cases for other encodings, make that an option through the command line or other means.

Upvotes: 1

Related Questions