First, understand 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, any one of the encoded strings to be converted to another encoding format must first be decoded and then encoded
Non-Unicode encoding--Unicode---Non-Unicode encoding
For example, the UTF-8 encoded string St wants to be converted 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 code is also divided into a BOM and no BOM.
IV: About the Chinese encoding of the JSON file. The Json.load () function is often used when reading JSON files in Python, which is required for the format of the JSON file
1) JSON file is utf-8 without BOM encoding, you can directly use the json.load (filename) function to read the contents of the JSON file
2) JSON file is utf-8 with BOM encoding, cannot be read with Json.load () function, json.load () does not recognize correctly
3) JSON file when other encoded, such as GBK, to the JSON file encoding format as a parameter to Json.load ():
eg. Json.load (filename, "GBK")
Five, how to view and set the encoding of their own files?
Introduce a personal favorite tool "nodtepad++", any software tube home with one click to install.
With this tool you can easily view the current encoding of your files and easily convert to any other encoding format