Character encoding for Python

Source: Internet
Author: User
Tags coding standards

All the data in the computer is a binary number

The default encoding format in Python2 is Ascci code, and the default encoding format in Python3 is UTF-8

You can view the current encoding format by using the following code:

Import Sys

Print (Sys.getdefaultencoding ()) #输出当前的编码格式

Character encoding Conversion
Many other countries have developed their own coding standards, but they do not support each other. This brings a lot of problems. Therefore, the international standard who organization for unified coding: The standard coding criteria: UNICODE.
Unicode is represented by two bytes as a single character, it can combine 65535 different characters in total, which is enough to cover all the symbols in the world (including Oracle)

UTF-8 (8-bit Unicode Transformation Format) is a variable-length character encoding for Unicode, which can represent a symbol using 1~4 bytes, depending on
Different symbols change the length of the byte, when the character is in the ASCII range, it is expressed in one byte, so it is compatible with ASCII code.

The obvious benefit is that while the data in our memory is Unicode, it is far less utf8 to use Unicode directly when data is saved to disk or used for network transmission!
This is also why UTF8 is our recommended coding method.

The relationship between Unicode and UTF8:
Word: Unicode is a memory-encoded representation scheme (a specification), and UTF is a scheme for how to save and transmit Unicode (implementation), which is also the difference between UTF and Unicode.

Conversion between UTF-8 and GBK:

Unicode (encode)--->utf-8
UTF-8 (decode)-->unicode

Unicode (encode)-->GBK
GBK (decode)-->unicode

The encoding format needs to be clearly defined in the decode () and encode for the current encoding format and the format to be converted to each other.

The default in Python3 is Utf-8


GBK conversion to UTF-8 format process:
1, first by decoding "decode" to convert to Unicode encoding
2. Then convert to UTF-8 by encoding "encode"
GBK (decode)-->unicode (encode)-->utf-8
Gbk.decode ("GBK"). Encode ("Utf-8")
Decode () The parameters are now encoded in the format, encode () the parameters are to be converted to the encoding format

UTF-8 conversion to GBK format flow:
1, first by decoding "decode" to convert to Unicode encoding
2. Then convert to UTF-8 by encoding "encode"
UTF-8 (decode)-->unicode (encode)-->GBK
Utf-8.decode ("Utf-8"). Encode ("GBK")


Note: In Py3 encode, the transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string

ImportSYSPrint(sys.getdefaultencoding ()) MSG="I love you china"#utf-8 quasi-change GBK in fact by a uft-8 encoded into the GBK formatS_to_gbk=msg.encode ("GBK")#The default is Utf-8, no more decode, hi Big BenPrint(S_TO_GBK)#GBK conversion Utf-8 GBK first decoded into Unicode format and then encoded into Uft-8S_to_utf8=s_to_gbk.decode ("GBK"). Encode ("Utf-8")Print(S_to_utf8)#Chinese output utf-8 re-decode outputUtf_print=msg.encode ("gb2312"). Decode ("gb2312"). Encode ("Utf-8"). Decode ("Utf-8")Print(Utf_print)
View Code

The output of the code

Character encoding for Python

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.