Python-2.7: Coding problems and encode and decode

Source: Internet
Author: User

Ordinary strings in the py2.7 are ASCII-encoded, for example, str= "ABC", if it contains Chinese will be GBK or gb2312 code (GB2312 is the Chinese provisions of Chinese character coding, also can be said to be Simplified Chinese character set encoding; GBK is an extension of GB2312, besides compatible GB2312, it can also display traditional Chinese, as well as kana in Japanese.

But by adding u to the string, such as Str=u "ABC", you can define the string as Unicode encoding

The system comes with the Code view can be obtained through sys.getdefaultencoding (), to modify the default encoding required first reload (SYS), because after the initialization will be removed sys.setdefaultencoding this method, we need to re-loaded, Then use Sys.setdefaultencoding (' Utf-8 ') to modify

When editing the Py file, add the #-*-Coding:utf-8-*-to the header to make the Py file Utf-8 encoded, which can contain Chinese

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

The role of encode is to convert Unicode encoding into other encoded strings, such as Str2.encode (' gb2312 '), which translates Unicode-encoded strings into gb2312 encodings.

Wakahara strings are not Unicode encoded and are encode directly, the system default encoding is decoded and then encode, which is Str2.encode (' gb2312 ') = = Str2.decode (sys.getdefaultencoding ()). Encode (' gb2312 ')

In the case of a utf-8 file, the string is utf-8 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.

Isinstance (Str,unicode) can determine whether a string is Unicode encoded, yes returns True, not return false

If you decode some encodings that contain special characters directly, you may throw exceptions that can be resolved in the following ways:

S.decode ("Utf-8", "ignore") ignores code that has an exception, showing only valid encodings
S.decode ("Utf-8", "replace") replace the code of the exception, this relative may know the character encoding problem at a glance, but will confuse the original code, more for testing, not very practical

Can see the GBK encoding decoding to correctly display Chinese can not be used utf-8 to encode, or only with GBK or gbk2312, speculated to open some txt or word garbled should be the GBK code utf-8 display

Various coding common Code reference: http://blog.csdn.net/shijing_0214/article/details/50908144

Python-2.7: Coding problems and encode and decode

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.