Joke Collection Website - Public benefit messages - What if python string is garbled?

What if python string is garbled?

The representation of strings in python is encoded in unicode.

Therefore, when encoding conversion, it is usually necessary to use unicode as the intermediate encoding, that is, first decode other encoded strings into unicode, and then encode them from unicode into another encoding.

Decode is used to convert other encoded strings into unicode encoding, such as string 1.decode('utf-8'), which means that utf-8 encoded string 1 is converted into unicode encoding.

The function of encode is to convert unicode encoding into other encoded strings, such as string2.encode('utf-8'), which means to convert unicode encoded string string2 into utf-8 encoding.

If a string is already unicode, it is wrong to decode it again, so it is usually necessary to judge whether its encoding method is unicode:

Isinstance(string3, unicode) # is used to determine whether string3 is encoded in unicode.

String3 encoding using non-unicode unicoded encoding will also cause errors.

It is recommended to learn Python tutorial!