Python encoding and conversion

Source: Internet
Author: User

===== 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.