Encode and decode of Python strings

Source: Internet
Author: User

Encode and decode methods for Python's Str,unicode objects
The Str object in Python is actually "8-bit string", a byte string, essentially similar to byte[in Java).
The Unicode object in Python should be the equivalent of a string object in Java, or essentially a Java char[].
For

s= " Hello  " u=u" hello "

1. The S.decode method and the U.encode method are the most commonly used,
In short, Python internally means that the string is Unicode (in fact, Python's internal representation and real Unicode are somewhat different, almost transparent to us, not considered), and the Str object is used when interacting with people.
S.decode--------> Decodes S to Unicode, and the parameter specifies s original encoding. This is the same as Unicode (S,encodename).

1. The S.decode method and the U.encode method are the most commonly used,
In short, Python internally means that the string is Unicode (in fact, Python's internal representation and real Unicode are somewhat different, almost transparent to us, not considered), and the Str object is used when interacting with people.
S.decode--------> Decodes S to Unicode, and the parameter specifies s original encoding. This is the same as Unicode (S,encodename).
U.encode--------> Encodes Unicode into a str object, and the parameter specifies the encoding used.
Mnemonic: Decode to Unicode from parameter
Encode to parameter from Unicode
Only the Decode method and the Unicode constructor can get Unicode objects.
The most common uses for this are such scenarios where we specify the use of the encoding cp936 in the Python source file,
# coding=cp936 or #-*-coding:cp936-*-or #coding:cp936 way (not write default is ASCII encoding)
So the Str object in the source file is cp936 encoded, and we want to pass this string to a place that needs to be saved to another encoding (such as the utf-16 required for XML utf-8,excel)
It's usually written like this:
Strobj.decode ("cp936"). Encode ("utf-16")

You typically encode a Unicode string whenever your need to use it for iOS, for instance transfer it over the network, or SA ve it to a disk file.
To convert a string of bytes to a Unicode string is known as decoding. Use Unicode (' ... ', encoding) or ' ... '. Decode (encoding).
You typically decode a string of bytes whenever to receive string data from the network or from a disk file.

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.

The role of Decode is to convert other encoded strings into Unicode encodings, such as Str1.decode (' gb2312 '), to convert gb2312 encoded string str1 into Unicode encoding.

The role of encode is to convert Unicode encoding into other encoded strings, such as Str2.encode (' gb2312 '), to convert 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

How do I get the default encoding for my system?

#!/usr/bin/env python
#coding =utf-8
Import Sys
Print sys.getdefaultencoding ()

The program is printed on the English windowsxp as: ASCII

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

Prompt: Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 0-1: Ordinal not 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 ')

Can correctly output "Chinese" two words.

If the last sentence should read: Print S.encode (' UTF8 ')

The output: \xe4\xb8\xad\xe6\x96\x87, which is the result of the Console Information Output window UTF8 encoded strings in ASCII encoded output.

Unicode (str, ' gb2312 ') is the same as Str.decode (' gb2312 '), which converts gb2312 encoded STR to Unicode encoding

Use str.__class__ to view the encoded form of STR

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.