===== Python encoding ======
This paper mainly introduces the conversion between the encoding mechanism of Python, Unicode, UTF-8, UTF-16, GBK, gb2312, ISO-8859-1 and so on.
** Common encoding conversions are divided into the following situations :**
===== Convert Unicode to other encodings (GBK, gb2312, etc.) ====
For example, a converts unicode encoding to gb2312. A. encode ('gb2312 ')
<Code Python>
#-*-Coding = gb2312 -*-
A = u "Chinese"
A_gb2312 = A. encode ('gb2312 ')
Print a_gb2312
</Code>
===== Convert other encodings (UTF-8, GBK) to Unicode ====
For example, if a is gb2312 encoded, it must be converted to Unicode. Unicode (A, 'gb2312') or A. Decode ('gb2312 ')
<Code Python>
#-*-Coding = gb2312 -*-
A = u "Chinese"
A_gb2312 = A. encode ('gb2312 ')
Print a_gb2312
A_unicode = a_gb2312.decode ('gb2312 ')
Assert (a_unicode =)
A_utf_8 = a_unicode.encode ('utf-8 ')
Print a_utf_8
</Code>
===== Conversion between non-Unicode encodings ====
Encoding 1 (GBK, gb2312) to encoding 2 (UTF-8, UTF-16, ISO-8859-1)
You can convert it to Unicode first and then to encoding 2.
For example, gb2312 to UTF-8
<Code Python>
#-*-Coding = gb2312 -*-
A = u "Chinese"
A_gb2312 = A. encode ('gb2312 ')
Print a_gb2312
A_unicode = a_gb2312.decode ('gb2312 ')
Assert (a_unicode =)
A_utf_8 = a_unicode.encode ('utf-8 ')
Print a_utf_8
</Code>
===== Judge the encoding of a string ====
Isinstance (S, STR) is used to determine whether it is a general string //
Isinstance (S, Unicode) is used to determine whether it is Unicode //
If a character string is Unicode, an error occurs when Unicode conversion is executed )//
The following code converts any string to Unicode
<Code Python>
Def U (S, encoding ):
If isinstance (S, Unicode ):
Return s
Else:
Return Unicode (S, encoding)
</Code>
===== Difference between Unicode and other encodings ====
Why do not all files use Unicode, but GBK, UTF-8, and Other encoding? //
Unicode can be called abstract encoding, that is, it is only an internal representation and cannot be directly saved. //
When saving to a disk, You need to convert it to the corresponding encoding, such as UTF-8 and UTF-16.
===== Other methods ====
In addition to the above encoding methods, you can also use the open method of codecs to convert data during reading and writing.