Python ASCII, GB2312, Unicode, UTF-8 Mutual conversion __python

Source: Internet
Author: User
Tags control characters locale locale setting
ASCII is a character set, including uppercase and lowercase letters, numbers, control characters, etc., which are represented by a byte, and the range is 0-127 Unicode into UTF-8 and UTF-16.
UTF-8 variable length, up to 6 bytes, less than 127 characters in a byte, as in the case of the ASCII character set, the English text under ASCII encoding does not need to be modified to be treated as a UTF-8 encoding.
Python supports Unicode starting at 2.2, and function decode (Char_set) implements other encodings to Unicode conversions, and function encode (Char_set) converts Unicode to other encodings.
Like what
Will get
U ' \u4f60\u597d ',
The "You" and "good" Unicode codes are 0x4f60 and 0x597d, respectively.
and then use
Will get
' \XE4\XBD\XA0\XE5\XA5\XBD '
It is the "hello" UTF-8 coding result.
The key to using Unicode in Python is that Unicode is a class that functions Unicode (str, "UTF8") generates an object of the Unicode class from the string str of the UTF8 encoding (or, of course, another encoding), while the function Unc.encode (" UTF8 ") converts the object UNC of a Unicode class to (encoded as) a string of UTF8 encoding (or, of course, another encoding). So, to write a Unicode-related program, what needs to be done is to generate Unicode objects with Unicode (str, "UTF8") when fetching data (strings) * Use Unicode objects only in the program, and the string constants that appear in the program are written as U "Strings" * Output, you can convert Unicode objects 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 
       Hello 

>>> print x.encode ("UTF8") 
       hello 

>>> print x.encode ("GBK") 
       

These are the test results (Ubuntu 6.06,locale is UTF8), and note the difference between type (x) and type ("Hello"). It can be seen from the coding that the UTF8 encoding differs from the GBK. Under UTF8 's locale setting, print x is encoded by the environment variable (i guess I guess I guess), while the print X.encode ("GBK") is garbled.

Turn from: http://www.blogjava.net/pts/archive/2008/01/20/176598.html

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.