Python encoding: Conversion of Unicode byte strings into Chinese strings

Source: Internet
Author: User

Coding problems have always been a headache:

 

When the string is '\ u4e2d \ u56fd'

>>> S = ['\ u4e2d \ u56fd', '\ u6e05 \ u534e \ u5927 \ u5b66']
>>> STR = s [0]. Decode ('unicode _ escape ') #. encode ("euc_kr ")
>>> Print Str

China

 

When the string is '& #19996; & #20122; & #23398; & #22242; & #19968; & #20013 ;'

>>> Print unichr (19996)

East

Ord () supports Unicode and can display Unicode numbers of specific characters, such:

 
>>> Print ord ('A') 65

Unicode strings are generated as long as they are connected to Unicode. For example:

 
>>> 'Help'> 'Help, '+ u'python' u'help, python'

 

For ASCII (7-bit)-compatible strings, you can convert Unicode strings into ASCII strings with the built-in STR () function.For example:

>>> STR (u'hello World') 'Hello world'

 

 

Understanding of several concepts:

ASCII code: Use data words to correspond to corresponding characters

 

The Chinese Character corresponds to the location code. For example, the "good" ASCII code is 22909.

 

Unicode encoding is divided into one field for each country. It has UTF-8, UTF-16, UTF-32 AND OTHER FORMS

The Chinese value range is 4e00-9fbf. GBK, gb2312,

 

UTF-8 is a Unicode-based international application.
Gb2312 and gb2312 are both early Chinese character codes used for encoding and decoding.

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.