Python Chinese character garbled

Source: Internet
Author: User
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'

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.