Python encodes those things in Chinese

  • 2020-04-02 13:45:58
  • OfStack

First, understand the difference between encode() and decode()

  Encode () is used to convert Unicode encoded strings into other encoding formats.

For example: st1. Encode (" utf-8 ")   This sentence encodes a Unicode encoded st1 to a utf-8 encoded string

Decode () converts strings from other encoding formats into unicode-encoded strings.

For example: st2.decode("utf-8") decodes the utf-8 encoded string st2 into a unicode-encoded string

Second, in addition to unicode-encoded strings, any encoded string must be decoded before it can be converted to any other encoding format

Non-unicode encoding -- > Unicode - > The Unicode

For example, to convert a utf-8-encoded string st to a gbk-encoded string, you must go through the following steps:

St =st.decode("utf-8") # decode to Unicode encoding

St = st. encode (" GBK ")   Code from Unicode to GBK

Third, the commonly used utf-8 codes are also classified as BOM and BOM free.

Fourth: the Chinese encoding of json file. The json.load() function is often used when reading a Json file in Python, and it requires the format of the Json file

1) the json file is encoded by utf-8 without BOM, so you can directly read the contents of the json file with the json.load(filename) function

2) json file is encoded by utf-8 with BOM, so it cannot be read by json.load() function, and json.load() cannot be correctly recognized

3) for json files with other encoding, such as GBK, the encoding format of json files should be passed to json.load() as a parameter:

Eg. The json. The load (filename, "GBK")

Fifth, how do you view and set your own file code?

Introduction of a personal favorite tool "Nodtepad++", any software tube at home with a key installation.

With this tool you can easily view the current encoding of your file and easily convert to any other encoding format


Related articles: