Conversion of individual character encodings in Python3

Source: Internet
Author: User

a= ' I'm okay ' # # # #python3 default encoding for Unicode
# # #unicode >gb2312
unicode_gb2312=a.encode (' gb2312 ') # # #因为默认是unicode所以不需要decode (), direct encode Chengxiang to convert the encoding such as gb2312
print (' My gb2312 ', unicode_gb2312) # # #返回结果: my gb2312 B ' \xce\xd2\xba\xdc\xba\xc3 '
# # #gb2312 >utf8
Gb2312_utf8=unicode_gb2312.decode (' gb2312 '). Encode (' Utf-8 ') # #当前字符为gb2312所以要先decode成unicode ( The parameter passed in decode is the encoding set of the current character and then encode into Utf-8
Print (' I'm Utf-8 ', Gb2312_utf8) # # #返回结果: I'm utf-8 B ' \xe6\x88\x91\xe5\xbe\x88\xe5\xa5\xbd '
# # #utf8 >GBK
utf8_gbk=gb2312_utf8.decode (' Utf-8 '). Encode (' GBK ')# # The current character set is encoded as Utf-8 to convert to GBK first decode into a Unicode character set and then encode to GBK character set
Print ("I am GBK", UTF8_GBK) # # #返回结果: I am gbk b ' \xce\xd2\xba\xdc\xba\xc3 '
# # #utf8 >uicode
utf8_unicode=utf8_gbk.decode (' GBK ') # # # #注意当转换成unicode时 do not need encode ()
Print (' I'm Unicode ', Utf8_unicode) # # #返回结果: I'm Unicode I'm fine.
# # #unicode >gb18030
unicode_gb18030=utf8_unicode.encode (' gb18030 ')
Print (' I'm gb18030 ', unicode_gb18030) # # #返回结果: I'm gb18030 b ' \xce\xd2\xba\xdc\xba\xc3 '

# # #总结各个编码的互相转换都要先转换成unicode然后通过unicode再转换成想要的编码
# #从上面可以看出gb2312, the results of gbk,gb18030 return are the same, it should be for these 3 are Chinese code, so are all downward compatible with each other
# #中国的编码最先出来的是gb2312, then GB18030, and finally GBK, the number of characters they support is also increasing in order from the first 7,000 to the present nearly 30,000

Conversion of individual character encodings in Python3

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.