Encode and decode of python strings

Source: Internet
Author: User

The first thing to figure out is that the representation of a string inside Python is Unicode encoding.

Therefore, in encoding conversion, it is often necessary to use Unicode as an intermediate encoding, that is, decoding other encoded strings (decode) into Unicode, and then encoding from Unicode (encode) to another.

The role of Decode is to convert other encoded strings to Unicode encoding.

such as Str1.decode (' gb2312 '), means that the gb2312 encoded string is converted to Unicode encoding.

The role of encode is to convert Unicode encoding into other encoded strings,

such as Str2.encode (' gb2312 '), means converting a Unicode-encoded string to GB2312 encoding.

In some Ides, the output of a string is always garbled, or even wrong, because the IDE's result output console itself cannot display the encoding of the string, rather than the problem of the program itself.

As in Ulipad, run the following code:

S=u "Chinese"

Print S

Will prompt:

Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 0-1: Ordinal isn't in range (128).

This is because ulipad on the English WindowsXP console information Output window is ASCII encoded output (the default encoding of the English system is ASCII), and the string in the above code is Unicode encoded, so the output generated an error.

Replace the last sentence with the following: Print S.encode (' gb2312 ') will correctly output the word "Chinese" two words.

If the last sentence should read: Print S.encode (' UTF8 ') output: \xe4\xb8\xad\xe6\x96\x87,

This is the result of the Console Information Output window UTF8 encoded strings in ASCII encoded output.

In addition, 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 the UTF8 file, the string is UTF8 encoding, 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, when you do not specify a specific encoding, you create a code file using the system default encoding, and in this article you can see how to get the system's default encoding.

If the string is defined as: S=u ' Chinese ' then 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.

Therefore, it is common to determine whether the encoding is Unicode:

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

Encode and decode of python strings

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.