Python ascii, GB2312, Unicode, UTF-8 convert each other

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

ASCII is a character set, including uppercase and lowercase letters, numbers, control characters, and so on, which is represented in a single byte, and the range is 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, and function decode (char_set) can implement other Encoding-to-unicode conversions, and function encode (char_set) to convert Unicode to other encoding methods.
Like what

Will get

U ' \u4f60\u597d ',
The Unicode codes for "you" and "good" are 0x4f60 and 0x597d, respectively.
Re-use

  

Will get

' \XE4\XBD\XA0\XE5\XA5\XBD '
It is the UTF-8 encoded result of "hello".
The key to using Unicode in Python: Unicode is a class, the function Unicode (str, "utf8") is generated from UTF8 encoding (or, of course, Another encoding) of the Unicode class of the string str, and the function Unc.encode (" UTF8 ") converts the object UNC of a Unicode class to a string (encoded as) UTF8 encoded (or, of course, another encoding). so, the thing to do when you write a unicode-related program is * get data (string), generate Unicode objects with Unicode (str, "utf8") * Use only Unicode objects in your program, and string constants appearing in your 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          Hello     >>> print x.encode ("utf8")          Hello     >>> 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.

Python ascii, GB2312, Unicode, UTF-8 convert each other

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.