The problem this time is still related to Python Unicode. Take a look at the following code:
The code is as follows |
Copy Code |
[~/tmp]$ Cat test.py #coding: UTF8 Foo = u ' Test ' Print Foo [~/tmp]$ python test.py Test [~/tmp]$ python test.py >/tmp/foobar.txt Traceback (most recent call last): File "test.py", line 5, in <module> Print Foo Unicodeencodeerror: ' ASCII ' codec can ' t encode Characters in position 0-1: Ordinal not in range (128) |
Simply print a Unicode object, the direct output will not be an error, but redirect to the file will hang off, this really makes people want to "hehe."
Google then found the answer on the StackOverflow. The original Python 2 print is automatically encode for Unicode parameters. If the target of the output is a terminal, it will use the terminal encoding (available via sys.stdout.encoding), and if the output target is a pipe (redirect or use a pipe character), the corresponding encoding cannot be obtained, at which point Python uses the system default encoding.
You can print sys.stdout.encoding view Terminal code:
The code is as follows |
Copy Code |
[~/tmp]$ python-c ' Import sys; Print Sys.stdout.encoding ' UTF-8 [~/tmp]$ python-c ' Import sys; Print Sys.stdout.encoding ' | Tee/tmp/foo.txt None
|
Sure enough, the terminal code is None in the case where the pipe is used.
The solution is also very simple, is to manually encode before print, such as in the Linux environment can be completely encode into UTF-8. If you want to do better, you can also judge a decision based on sys.stdout.encoding and whether there is a redirect.
When I first started blogging I ran into a Unicode-formatted pit, and I didn't expect to see a similar problem two years from now. I have to say the default encoding for Python 2 and the Unicode and STR blocks are easy to get out of the problem. Unfortunately, although Python 3 solves these problems perfectly, it is a pity that they are 3.X or not widely available.