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