Go Python ascii, GB2312, Unicode, UTF-8

Source: Internet
Author: User
Tags control characters locale locale setting

2007-12-13 10:50:47| Category: Python utility software compilation | Report | Font size Subscription ASCII is a character set, including uppercase and lowercase letters, numbers, control characters, etc., which are represented by a byte, with a range of 0-127

Unicode is divided into UTF-8 and UTF-16. UTF-8 variable length, up to 6 bytes, characters less than 127 are expressed in one byte, and as with the ASCII character set, the English text under ASCII encoding does not need to be modified to be processed as UTF-8 encoding.

Python supports Unicode starting from 2.2,function decode (Char_set) can implement other encoding-to-Unicode conversions,function encode (char_set) enables conversion of Unicode to other encoding methods。

For example ("Hello"). Decode ("GB2312") will get U ' \u4f60\u597d ', i.e. the Unicode codes for "You" and "good" are 0x4f60 and 0x597d respectively
Re-use (U ' \u4f60\u597d '). Encode ("UTF-8") will get ' \xe4\xbd\xa0\xe5\xa5\xbd ', which is the UTF-8 coding result of "Hello".


The key to using Unicode in Python:Unicode is a class that functions Unicode (str, "UTF8") from the UTF8 encoding (or, of course, another encoding) of the string str to generate the Unicode class object, whileThe function Unc.encode ("UTF8") converts the object UNC of the Unicode class to a string (encoded as) UTF8 encoded (or, of course, another encoding)。 So, the thing you need to do to write Unicode-related programs is

* When getting data (string), use Unicode (str, "UTF8") to generate Unicode objects
* Only Unicode objects are used in the program, and string constants appearing in the program are written in the form of U "string"
* Output, the Unicode object can be converted to any encoded output, using Str.encode ("some_encoding")

>>> Unicode ("Hello", "UTF8")
U ' \u4f60\u597d '
>>> x = _
>>> type (x)

>>> type ("Hello")

>>> x.encode ("UTF8")
' \XE4\XBD\XA0\XE5\XA5\XBD '
>>> X.encode ("GBK")
' \xc4\xe3\xba\xc3 '
>>> X.encode ("gb2312")
' \xc4\xe3\xba\xc3 '
>>> Print X
How are you doing
>>> print X.encode ("UTF8")
How are you doing
>>> print X.encode ("GBK")
???

The above is the test result (Ubuntu 6.06,locale is UTF8), note the difference between type (x) and type ("Hello"). It can be seen from the coding that UTF8 encoding differs from GBK. In the locale setting of UTF8, print x is encoded by the environment variable (i guess I guess I guess), while the print X.encode ("GBK") is garbled.

Go Python ascii, GB2312, Unicode, UTF-8

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.