Some problems encountered in the process of Python encoding in Chinese

Source: Internet
Author: User

First, make clear the difference between encode () and Decode ()

The role of Encode () is to convert Unicode-encoded strings to other encoding formats.

For example: St1.encode ("Utf-8") is the function of encoding Unicode encoded ST1 into a utf-8 encoded string

The role of Decode () is to convert strings from other encoded formats into Unicode-encoded strings.

For example: St2.decode ("Utf-8") is the function of decoding UTF-8 encoded string ST2 into a Unicode encoded string

Second, in addition to Unicode-encoded strings, no matter what kind of encoded string you want to convert to another encoding format, you must first decode and then encode

Non-Unicode encoding--Unicode---Non-Unicode encoding

Like what. The UTF-8 encoded string St wants to be converted to a GBK encoded string. You must go through the following steps:

St=st.decode ("Utf-8")#解码为Unicode编码

St=st.encode ("GBK") #从Unicode编码编码为gbk编码

Third. We often use the Utf-8 code is also divided into a BOM and no BOM.

To participate in this article:

http://blog.csdn.net/lipeijs3/article/details/5062243

IV: About the Chinese encoding of the JSON file.

Using Python to read JSON files is often used to the json.load () function, which is required for the format of the JSON file

1) JSON file is utf-8 without BOM encoded, then can directly use the json.load (filename) function to read the contents of the JSON file

2) The JSON file is encoded with the Utf-8 with BOM and cannot be read with the Json.load () function. Json.load () does not correctly identify

3) JSON file when other encoding, for example GBK, to the JSON file encoding format as a parameter to Json.load ():

eg. Json.load (filename, "GBK")

Five, how to view and set their own file encoding it?

Introduce a personal favorite tool "nodtepad++", any software tube home with a button installed.

With this tool you can easily view the current encoding of your files. and can easily be converted into arbitrary other coding formats

Some problems encountered in the process of Python encoding in Chinese

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.