Python Chinese output garbled problem how many beginners, I planted in this area do not know how many somersault. Now I write about the problems and solutions I have come up with to share with you:
1 output garbled
The so-called garbled is refers to "contact language ionizing Juan Po Ying 楃 Juan" such content. Why does it output such a result? Because Python's input and input emphasizes the principle of "inconsistent access". The so-called "discrepancy" refers to what kind of code comes in but does not necessarily output with that encoding. Why is it? Because the Python IDE is only supported for Unicode output, if we are from the file Open data is Utf-8 or ASCII, the output is Unicode output, the result will of course be garbled. So how to solve it?
(1) We do text analysis when the general text file to UTF8 encoding, so first read the text file encoding method set to UTF8, this step must be changed, otherwise how to change the code has no effect;
(2) In the Python header section first join:
#-*-Coding:utf-8-*-
(3) Then add the following three words to the file header:
Import (SYS)
Reload (SYS)
Sys.setdefaultencoding (' Utf-8 ')
This setdefaultencoding is set when the system starts, that is, when you enter Python >>> in the console, the system automatically reads a file, so you must re-reload when you are using Python.
The input will then be successful.
2 Output UTF8 encoding
Output UTF8 encoding This problem I have not solved, the current approach is to import into the TXT file ...
3 Output List garbled
If the list is garbled, the contents of the list are read out and connected to a string:
#print ' [' + ', '. Join (name) + '] '
Python character output coding problem