Python Decoding and encoding

Source: Internet
Author: User

Decode is decoding, encode encoding

ASCLL in Python2 by default when Unicode is default in Python3

GBK steering Utf-8: First decode the GBK into Unicode, encoded into utf-8.

Utf-8 steering GBK: First decode the utf-8 into Unicode, encoded into GBK.

Python2 Code:

1 #-*-coding:utf-8-*-2 3 " "4 @auther: Starry5 @file: py2ende.py6 @time: 18-1-12 pm 9:527 " "8 9 " "Ten ASCII is the default in Python2 One " " A ImportSYS -  - Print(Sys.getdefaultencoding ()) the  -s ='Hello' -  -S_unicode = S.decode ('Utf-8') +S_GBK = S_unicode.encode ('GBK') -  + Print(S_unicode) A Print(S_GBK) at  -Gbk_to_utf8 = S_gbk.decode ('GBK'). Encode ('Utf-8') - Print(Gbk_to_utf8)

Python3 Code:

1 #!/usr/bin python3.52 #-*-coding:utf-8-*-3 4 " "5 @auther: Starry6 @file: py3ende.py7 @time: 18-1-12 pm 9:568 " "9 Ten " " One default Unicode in Python3 A " " -  - ImportSYS the Print(Sys.getdefaultencoding ()) -  -  -s ='Hello' + Print(s) -  +  AS_utf8 = S.encode ('Utf-8') atS_GBK = S.encode ('GBK') -  - Print(S_utf8) - Print(S_GBK) -  -Gbk_unicode = S_gbk.decode ('GBK') in Print(Gbk_unicode) -  toUTF8_TO_GBK = S_utf8.decode ('Utf-8'). Encode ('GBK') +Gbk_to_utf8 = S_gbk.decode ('GBK'). Encode ('Utf-8') -  the Print(UTF8_TO_GBK) * Print(Gbk_to_utf8)

Python Decoding and encoding

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.