Python Chinese encoding conversion and correct output

Source: Internet
Author: User
Tags in python

The default encoding for strings in Python code is the same as the encoding of the code file itself
The role of Decode is to convert other encoded strings into Unicode encoding
The role of encode is to convert a Unicode encoding into another encoded string

>>> s= "Chinese"
>>> s
' Xd6xd0xcexc4 '
>>> S.decode ("GBK")
U ' u4e2du6587 '
>>> print S.decode ("GBK")
Chinese
>>> Print S
Chinese
>>> S.decode ("GBK"). Encode ("GBK")
' Xd6xd0xcexc4 '
>>> print S.decode ("GBK"). Encode ("GBK")
Chinese
>>>

>>> a= ' Chinese '
>>> A
' XD6XD0XB9XFAXC8XCB '
>>> B = Unicode (A, ' GBK ')
>>> b
U ' U4e2du56fdu4eba '
>>> a.find (' Middle ')
0
>>> a.find (' People ')
4
>>> b.find (' People ' decode (' GBK '))
2
>>> Print a
Chinese
>>> Print B
Chinese

Here the Find function found that the results should be very well understood. Find in B must be decode, otherwise there will be an error. As for why print is Chinese characters, I have not yet studied it out. Please tell the master.

>>> b.encode (' GB18030 ')
' XD6XD0XB9XFAXC8XCB '
>>> b.encode (' cp936 ')
' XD6XD0XB9XFAXC8XCB '
>>> b.encode (' GBK ')
' XD6XD0XB9XFAXC8XCB '
>>> b.encode (' Utf-8 ')
' Xe4xb8xadxe5x9bxbdxe4xbaxba '

Description GB18030, GBK, gb2312 and cp936 can be encoded in Chinese, and the results are consistent, utf-8 can also, but the encoding is not the same, so the results are different. They are all consistent in principle.

>>> type (a)
<type ' str ' >
>>> type (b)
<type ' Unicode ' >
>>> type (b.encode (' Utf-8 '))
<type ' str ' >

Shows that Python has two encodings for strings, one is the normal way, and the other is Unicode. Note that Utf-8 is also considered a common coding method.

Edit conversions are provided below

Chinese characters are converted into HTML entity characters.
Echo mb_convert_encoding ("re-play Once", "html-entities", "gb2312");

Encoding Conversion
s = "Chinese"
S1 = u "Chinese"

Unicode-> GBK
 s1.encode ("GBK")
Unicode-> utf-8
 s1.encode ("UTF-8")
GBK-> Unicode
 unicode (S, "GBK")
  or
 s.decode ("GBK")

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.