Str,unicode and gb2312 in Python

Source: Internet
Author: User

Example 1:

V1=u ' good magic question!? '

Type (v1)-"Unicode

V1.decode ("Utf-8") # Work,because V1 is Unicode already

V1.encode ("gb2312") #work, convert from Unicode to gbk2312

Found

Decode is the conversion of the specified object to Unicode (Unicode contains utf-8,utf-16) and indicates how the object to be converted is encoded.

Encode is the process of translating Unicode objects into other encodings.

Example 2:

V1=r ' C:\abc\1.txt '

V2=r ' good magic question '

V3=r ' C:\abc\12dc\ text-txt '

V4=r ' \\rdf\azzwang\maya2013 project\imageshahah\ æ refer Chuan lu cun trajectory. jpg '

. Decode ("Utf-8") . Decode ("gb2312")
V1
V2 X
V3 X
V4 X

Found

For a string of type str, if you want to use Utf-8 to decode it is completely impossible. Therefore, we expect a method of a program to return a specific encoding (such as Unicode), in which case we can use the Encode method directly into the encoding we want, without having to decode the puzzle.

Str,unicode and gb2312 in Python

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.