2

I am wondering why i can use print to print a unicode string in my OSX Terminal.app, but if i redirect stdout to a file or pipe it to 'more', i get an UnicodeEncodeError. How does python decides whether it prints unicode or throws an exception.

1
  • Can you please give a short example script that shows the difference in behavior? Commented Aug 10, 2011 at 15:26

1 Answer 1

2

Because your terminal encoding is set correctly and when you redirect to a file (or pipe) the encoding is set to the default encoding (ASCII in python2.) try print sys.stdout.encoding in both time (when you run your script as the terminal as stdout and when you redirect to a file) and you will see the difference.

Try also this in your command line:

$ python -c 'import sys; print sys.stdout.encoding;'
UTF8
$ python -c 'import sys; print sys.stdout.encoding;' | cat
None

More Info can be found HERE:

Sign up to request clarification or add additional context in comments.

3 Comments

Use the environment variable PYTHONIOENCODING if you want to control then encoding of the pipe.
Programs should not behave differently when run over a pipe. This is a yet another stupid Unicode-related bug where Python curls over backwards to appease burgeoning pods of hysterical porpoises. It should be fixed.
@tchrist: This issue was solved in python3, where the default encoding is no more ASCII.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.