Python (19) coding issues

Source: Internet
Author: User

Reprint Link: http://blog.csdn.net/lxdcyh/article/details/4018054

Python's coding problem has always been a headache, and today it's time to sort out Python's coding knowledge.

First: The representation of a string inside Python is Unicode encoding, so in encoding conversion, it is usually necessary to use Unicode as the intermediate encoding, that is, decoding the other encoded string (decode) into Unicode first. From Unicode encoding (encode) to another encoding.

decode: The effect is to convert other encoded strings into Unicode encodings, such as Str1.decode (' gb2312 '), Represents the conversion of GB2312 encoded string str1 to Unicode encoding.

encode: The role of converting Unicode encoding into other encoded strings, such as Str2.encode (' gb2312 '), is to convert the Unicode encoded string str2 to gb2312 encoding.

Therefore, the transcoding must first understand, the string str is what encoding, and then decode into Unicode, and then encode into other encodings.

The default encoding of the string in the code is consistent with the encoding of the code file itself.

such as: s= ' Chinese '

If it is in a UTF8 file, the string is UTF8 encoded, and if it is in a gb2312 file, it is encoded as gb2312. In this case, to encode the conversion, you need to first convert it to Unicode encoding using the Decode method, and then use the Encode method to convert it to another encoding. Typically, you create a code file by using the system default encoding when you do not specify a specific encoding method.

If the string is defined like this: S=u ' Chinese '

The encoding of the string is specified as Unicode, which is the internal encoding of Python, regardless of the encoding of the code file itself. Therefore, for this case to do the encoding conversion, only need to directly use the Encode method to convert it to the specified encoding.

If a string is already Unicode, then decoding will be an error, so it is common to determine whether it is encoded as Unicode:

Isinstance (S, Unicode) #用来判断是否为unicode

Encode with a non-Unicode encoded form of STR will error

Python (19) coding issues

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.