Python character encoding and conversion tips

Source: Internet
Author: User
Python has two types of strings: strobject and unicodeobject, both of which can store the byte encoding of characters, but they are different types, which is very important, it is also the reason why python has str object and unicode object strings that can both store character byte encoding, but they are different types. this is very important and is also why there is encode and decode.

The meaning of encode and decode in pyhton can be expressed

Encode

Unicode ---------------------------> str

Unicode <-------------------------- str

Decode

Several common methods:

Str_string.decode ('codec') is to convert str_string to unicode_string. codec is the encoding method of the source str_string.

Unicode_string.encode ('codec') is to convert unicode_string to str_string, and codec is the encoding method of the target str_string.

Str_string.decode ('from _ Codec'). encode ('to _ Codec') can be converted between str_strings of different encodings.

For example:

>>> T = 'Great Wall'

>>> T

'\ Xb3 \ xa4 \ xb3 \ xc7'

>>> T. decode ('gb2312'). encode ('utf-8 ')

'\ Xe9 \ x95 \ xbf \ xe5 \ x9f \ x8e'

Str_string.encode ('codec') first calls the system's default codec to convert str_string to unicode_string, and then converts it to the final str_string using the codec parameter of encode. equivalent to str_string.decode ('sys _ Codec '). encode ('codec ').

Unicode_string.decode ('codec') is basically meaningless. unicode only uses one unicode encoding type in python, UTF16 or UTF32 (determined when compiling python), and there is no need for encoding conversion.

Note: By default, codec is specified in the sitecustomize. py file under site-packages, for example

Import sys

Sys. setdefaultencoding ('utf-8 ')

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.