The difference between Python version and encoding

Source: Internet
Author: User

Main coding Introduction

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)

ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.

It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode

Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...

GBK, is also the compression and optimization of Unicode encoding, the full name of the Chinese character Code expansion code, the use of a double-byte encoding scheme, by the China Information Technology Standardization Committee developed.

Therefore, when the PYTHON2 interpreter loads the code in the. py file, the content is encoded (the default Ascill), if the following code is the case:

Error: ASCII code cannot be expressed in Chinese

1 #!/usr/bin/env python2   3 print "Hello, World"

Correction: It should be shown to tell the Python interpreter what code to use to execute the source code, i.e.:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3   4 print "Hello, World"

In the Python3, instead of using the default ut-8 encoding, so in Python3 no #-*-Coding:utf-8-*-will not be garbled.

Conversion between encodings

In the python2.7

If you want to convert UTF-8 to GBK encoding, first decode to Unicode and then encode GBK encoding, i.e.:

1 temp =  "Li Cheng" 2 # decoding needs to specify what encoding is originally 3 Temp_unicode = Temp.decode ("Utf-8 ') 4 # encoding needs to specify what encoding to encode 5 TEMP_GBK = Temp_unicode.encode ("GBK")

And in the Python3,

By removing the Unicode type encoding, the system automatically helps you to complete the internal series of conversions, which you only need to encode in one step:

1 temp = "Li Cheng" 2 # automatic conversion 3 TEMP_GBK = Temp.encode ("GBK")
Tips:

The default encoding format for window terminals is GBK, so UTF-8 encoded code is garbled at the terminal, but window can automatically convert Unicode encoded code into the encoding format it wants. So you just need to convert other types of encodings into Unicode on the window.

The difference between Python version 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.