Why is sys.stdout.encoding different when a channel exits (in Python2.x)?

When I run the same code with different pipelines, why is the output different?

% python2.7 -c 'import sys; print sys.stdout.encoding' UTF-8 % python2.7 -c 'import sys; print sys.stdout.encoding' | cat None 
+4
source share
1 answer

Because when you use cat (or any channel), you untie the process from the terminal. Python gets encoding information from terminal settings.

You can force the encoding to use an environment variable:

 export PYTHONIOENCODING=utf-8 
+7
source

All Articles