First, understand the difference between encode () and Decode ()
The role of Encode () is to convert a Unicode-encoded string to another encoding format.
For example: St1.encode ("Utf-8") is the function of encoding a UNICODE encoded ST1 as a UTF-8 encoded string
The role of Decode () is to convert strings of other encoded formats into Unicode-encoded strings.
For example: St2.decode ("Utf-8") is a sentence that decodes a utf-8 encoded string st2 into a Unicode-encoded string
Second, in addition to Unicode-encoded strings, any encoded string that you want to convert to another encoding format must be decoded before encoding
Non-Unicode encoding--> Unicode encoding--> non-Unicode encoding
For example, the UTF-8 encoded string St wants to convert to a GBK encoded string, which 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 coding is also divided into BOM and no BOM.
IV: The Chinese encoding of the JSON file. The Json.load () function is often used to read JSON files in Python, and the function is required for JSON file format
1 JSON file is utf-8 without BOM encoding, you can read the contents of the JSON file directly with the json.load (filename) function
2 JSON file is utf-8 with BOM encoding, can not be read with the Json.load () function, Json.load () is not recognized correctly
3 JSON file other encoding, such as GBK, to the JSON file encoding format as a parameter to the Json.load ():
eg. Json.load (filename, "GBK")
Five, how to view and set the encoding of their own files?
Introduction of a personal favorite tool "nodtepad++", a random software tube home with a key installation.
With this tool you can easily view the current encoding of your files and easily convert to any other encoding format