[Python Learning] Encoding

Source: Internet
Author: User

I. Source file code (ENCODING:UTF-8)

1.

Python 2.x by default by ASCII encoding read source files, the source code in the ASCII can not be represented by the character "", so error (3.x version does not error).

2.

The source code is added with Encoding:utf-8, which allows non-ASCII characters to appear in the original file, but is also transcoded when output in Windows DOS (GBK is converted here).

3.

Both the encoding of the source file and the output code of the DOS are GBK, and the content is consistent.

Ditto.

Second, string encoding

1. The default encoding for string storage:

Python 2.7

Python 3.3.3

2. Unicode object and normal string str

For S, press Utf-8 to decode to the Unicode object, and then encode the Unicode object by GBK.

A direct error in GBK encoding of the STR string is because the default encoding of the string (Python 2.7) is ASCII and the default is S.decode (' ASCII '). Encode (' GBK '), so an error occurs.

Set the string default encoding to Utf-8, no more error.

In Python we use decode () and encode () to decode and encode
In Python, the Unicode type is used as the underlying type of the encoding. That
Decode encode
STR--------->unicode--------->str

For Python 2.x, Python 3.x has changed, one version of a language.

Third, basestring, str, Unicode

3.1 STR and Unicode are subclasses of basestring:

3.2 Create a Unicode object using str:

See also: http://www.cnblogs.com/evening/archive/2012/04/19/2457440.html

[Python Learning] 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.