"Original" Python Chinese Encoding problem: Control window can output Chinese, to text file garbled

Source: Internet
Author: User
Tags python script

Encounter a problem: Control window can output Chinese,> to text file is garbled. Finally solved, summed up the Python Chinese code of the pit.

Start feeling very strange, want to first act what is normal? Then notice that the text file is ANSI encoded, the first line is output in the form of a string in the code, and the text format of the code is ANSI.

The data that is not normal at the back is output in the list with Utf-8 encoding. Beginning to want to output the first line with Utf-8, change the code to S.decode (' GBK '). Encode (' Utf-8 '), found the result or not. Because the string entered directly is handled according to the encoding of the code file.

The data in the list is then processed with the STR (i). Decode (' Utf-8 '). Encode (' GBK ') and the result is correct.


Additionally encountered an error: SyntaxError: (Unicode error) ' UTF8 ' codec can ' t decode byte 0xc0 in position 0:invalid start byte

Problem reason: The script file used by the encoding is ANSI, rather than UTF-8 encoding, so, U "Chinese", not recognized, and go to notepad+, format, and to Utf-8 encoding, and then save the file, then run the script, you can support U "Chinese".

Of course, the corresponding Unicode ("Chinese") approach, the starting point is the safest, and it will not be affected by the encoding used in your current Python script file.

"Original" Python Chinese Encoding problem: Control window can output Chinese, to text file garbled

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.