Python encoding method summary

Source: Internet
Author: User

Python encoding method summary (python uses unicode encoding internally ):

>>> T = 'beijing'

>>> T

'\ Xe5 \ x8c \ x97 \ xe4 \ Xba \ xac'

1) urllib. Quote (t) converts t to hexadecimal Encoding

'% E5 % 8C % 97% E4 % Ba % AC'

Urllib. Unquote ('% E5 % 8C % 97% E4 % Ba % AC') convert hexadecimal encoding to internal Encoding

'\ Xe5 \ x8c \ x97 \ xe4 \ Xba \ xac'

2) decode is used to convert other encoded strings to unicode encoding.

>>> S = T. Decode ('utf-8 ')

>>> S

U' \ u5317 \ u4eac'

>>> W = T. encode ('utf-8 ')

Traceback (most recent call last ):

File "<stdin>", line 1, in <module>

Unicodedecodeerror: 'ascii 'codec can't decode byte 0xe5 in position 0: ordinal not in range (128)

>>> W = S. encode ('utf-8 ')

>>> W

'\ Xe5 \ x8c \ x97 \ xe4 \ Xba \ xac'

>>> Q = S. encode ('gb2312 ')

>>> Q

'\ Xb1 \ xb1 \ xbe \ xa9'

First, we need to clarify that the character strings in Python are represented by Unicode encoding.

Therefore, during encoding and conversion, Unicode is usually used as the intermediate encoding, that is, decoding other encoded strings into Unicode, and then from the Unicode encoding (encode) into another encoding.

Decode is used to convert other encoded strings to unicode encoding,

For example, str1.decode ('gb2312') indicates converting a gb2312 encoded string to a unicode encoded string.

Encode is used to convert unicode encoding to other encoded strings,

For example, str2.encode ('gb2312') indicates converting a Unicode-encoded string to gb2312 encoding.

In some ides, the output of strings is always garbled or even incorrect. In fact, the IDE result output console itself cannot display the encoding of strings, rather than the program itself.

For example, run the following code in ulipad:

S = u "Chinese"

Print s

Will prompt:

Unicodeencodeerror: 'ascii 'codec can't encode characters in position 0-1: ordinal not in range (128 ).

This is because the ulipad console information output window on Windows XP is output according to ASCII encoding (the default English system encoding is ASCII), and the strings in the above Code are unicode encoded, therefore, an error occurs during output.

Change the last sentence to print S. encode ('gb2312') to correctly output the word "Chinese.

If the last sentence is changed to print S. encode ('utf8'), the output is \ xe4 \ xb8 \ XAD \ xe6 \ x96 \ x87,

This is the result of utf8 encoding string output in the console information output window according to ASCII encoding.

In addition, the default encoding of strings in the Code is the same as that of the code file,

For example, if S = 'China' is in the utf8 file, the string is UTF-8 encoded. If it is in the gb2312 file, it is encoded as gb2312. In this case, you must first use the decode method to convert the encoding to unicode encoding, and then use the encode method to convert it to other encoding.

Generally, when no specific encoding method is specified, it is the code file created using the system default encoding. In this article, we can see how to obtain the default encoding of the system.

If the string is defined as follows: S = u'chinese', the encoding of the string is specified as Unicode, that is, the internal encoding of Python, which is irrelevant to the encoding of the code file.

Therefore, to perform encoding conversion in this case, you only need to directly use the encode method to convert it to the specified encoding.

If a string is Unicode and decoded, an error occurs,

Therefore, we usually need to determine whether the encoding method is Unicode:

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.