Python ASCII, GB2312, Unicode, UTF-8 __python

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 are expressed in a byte range of 0-127

Unicode is divided 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.

For example ("Hello"). Decode ("GB2312") will get U '/u4f60/u597d ', that is, "you" and "good" Unicode codes are respectively 0x4f60 and 0x597d
Re-use (U '/u4f60/u597d '). Encode ("UTF-8") will get '/xe4/xbd/xa0/xe5/xa5/xbd ', which is the "hello" UTF-8 encoded 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, the thing to do is

* Generates Unicode objects with Unicode (str, "UTF8") when fetching data (strings)
* Use only Unicode objects in the program, 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 ' Unicode ' >
>>> type ("Hello")
<type ' str ' >
>>> 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")
???

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.

from:http://guanjh.javaeye.com/blog/131185

GB-----> ' utf_8

Write at the beginning of the program
#-*-Coding:utf-8-*-
Or
#-*-coding:gb18030-*-

In addition, it is recommended to write documents with UTF8,
Again, when you need GB, please use GB18030 instead of gb2312.

>>> gbstring = "Hello"
>>> gbstring.decode (' GBK '). Encode (' Utf_8 ')
'/XE4/XBD/XA0/XE5/XA5/XBD '

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.