Python Unicode to Chinese and conversion default encoding

Source: Internet
Author: User

One

Crawling web information in crawlers often requires converting a similar "\u4eba\u751f\u82e6\u77ed\uff0cpy\u662f\u5cb8" to Chinese, which is actually Unicode's Chinese encoding. The following methods can be used to convert:

1.

1 >>> s = u'\u4eba\u751f\u82e6\u77ed\uff0cpy\u662f\u5cb8'2 Print s 3 Life is short, py is shore

2.

1 >>> s = R'\u4eba\u751f\u82e6\u77ed\uff0cpy\u662f\u5cb8'2 >>> s = s.decode ('unicode_escape')3print  s  4 Life is short, py is shore

Two

In addition, the Python2 character encoding problem often encounters "unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 0-5: Ordinal not in Ran code error for GE (+).

The following methods can usually be used to solve the problem:

1 Import SYS 2 Reload (SYS) 3 sys.setdefaultencoding ('utf-8')

This method changes the default encoding ASCII of Python2 to Utf-8. But this method is not once and for all, and may cause some code behavior to become weird.

You can refer to this connection: http://blog.ernest.me/post/python-setdefaultencoding-unicode-bytes

Python Unicode to Chinese and conversion default encoding

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.