Python Unicode string Problem understanding

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.