I plan to use Python to parse network log files. Chinese garbled characters are inevitable. I used to learn Java and used Python to solve Chinese garbled text. I really don't know how to get started. The code that has been debugged under Java makes me angry when I put it in the Python environment. I finally solved the problem with the help of an Oschina. The main idea of Python encoding is: 1. string 2. Unicode3. the target log set (cp936... it is intended to use Python to parse the network log file.
Chinese garbled characters are inevitable.
I used to learn Java and used Python to solve Chinese garbled text. I really don't know how to get started.
The code that has been debugged under Java makes me angry when I put it in the Python environment.
I finally solved the problem with the help of an Oschina.
The main idea of Python encoding is:
1. string
2. Unicode
3. target sequence set (CP936, GB2312, GBK, and GB18030 seem to be directly convertible to each other, and most of the codes are the same)
To understand the Python system encoding structure, first read the following code:
Import locale def p (f): print '% s. % s (): % s' % (f. _ module __, f. _ name __, f () # returns the default character encoding p (sys. getdefaultencoding) # returns the encoding p (sys. getfilesystemencoding) # obtain the default region settings and return the ancestor (language, encoding) p (locale. getdefalocallocale) # return the user-defined text data encoding # this function only returns a guess p (locale. getpreferredencoding) # \ xba is the GBK encoding of 'hang' # mbcs is not recommended, here, only tests show why print r "'\ xba' should not be used '. decode ('mbcs '): ", repr (' \ xba \ xba '. decode ('mbcs ') # result on the author's Windows (region is set to Chinese (Simplified, Chinese) # sys. getdefaultencoding (): gbk # sys. getfilesystemencoding (): mbcs # locale. getdefalocallocale (): ('zh _ cn', 'cp936 ') # locale. getpreferredencoding (): cp936 # '\ xba \ xba '. decode ('mbcs '): u' \ u6c49'