Second, the life is short, I use Python "second article"

Source: Internet
Author: User

1 encoding

When the Python interpreter loads the code in the. py file, the content is Ascill encoded by default, so there is an error in Chinese, so you need to tell the Python interpreter what code to use to execute the source code. )

ASCII code, mainly used to display modern English and other Western European languages, which can be represented at most 8 bits (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.

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), i.e. 2 **16 = 65536.

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 ...

Problem: Python program is garbled under Windows terminal

The default encoding for Windows Gbk,linux is UTF-8.

Utf-8 GBK in python2.7 must first be converted to Unicode and then converted from Unicode to GBK.

  

 

Decode: Decoding

Encode: Encoding

The Python3 can be transferred directly from Utf-8 to GBK. Python3 will first convert Utf-8 to Unicode and then to GBK by Unicode. (There is no Unicode in Python3.)

Second, the life is short, I use Python "second 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.