Python---character encoding and transcoding

Source: Internet
Author: User

1. In Python2:

#-*-coding:utf-8-*-import Sysprint (sys.getdefaultencoding ()) #获取系统默认编码 #1.utf-8 Turn gbks = "Hello" S_to_unicode = S.D Ecode ("Utf-8") #将utf-8 type Convert to Unicodeprint (s_to_unicode) print (Type (s_to_unicode)) S_TO_GBK = S.decode ("Utf-8"). Enco De ("GBK") #utf-8 to GBK: Decode Utf-8 first into Unicode, encode into Gbkprint (S_TO_GBK) #注: Unicode can print utf-8 encoded characters directly #2.gbk to Utf-8gbk_ To_utf8 = S_to_gbk.decode ("GBK"). Encode ("Utf-8") print (Gbk_to_utf8)

2. In Python:

Import Sysprint (sys.getdefaultencoding ()) #获取系统默认编码utf-8 (Ignore) msg = "Hello" #默认就是unicode, no more Decodeprint (msg) msg_gb2312 = Msg.encode ("gb2312") #unicode转成gb2312, turn gb2312 to bytes type Prin T (msg_gb2312) Gb2312_to_unicode = Msg_gb2312.decode ("gb2312") #gb2312转unicodeprint (Gb2312_to_unicode) gb2312_to_ UTF8 = Msg_gb2312.decode ("gb2312"). Encode ("Utf-8") #gb2312转utf-8, turn into utf-8 and convert to bytes type print (GB2312_TO_UTF8)



Python---character encoding and transcoding

Related Article

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.