Python decode Unicode encode

Source: Internet
Author: User

The representation of a string inside Python is Unicode encoding, so in encoding conversion, it is usually necessary to use Unicode as the intermediate encoding, that is, decoding the other encoded string (decode) into Unicode first. From Unicode encoding (encode) to another encoding.

The default encoding of the string in the code is the same as the code file itself, and the following are two inconsistencies:

1. S = U ' Hello '

The encoding of the string is specified as Unicode, which is the internal encoding of Python, and is encoded with the code file itself (view default encoding: Import sys print (' Hello ', sys.getdefaultencoding ()) ASCII. Set default encoding:import sys reload (SYS) sys.setdefaultencoding (' Utf-8 ')) is irrelevant. Therefore, for this case to do the encoding conversion, only need to directly use the Encode method to convert it to the specified encoding.

2. #-*-Coding:utf-8-*-

s = ' Hello '

This is UTF-8 encoding, ASCII encoding cannot display Chinese characters

Isinstance (S, Unicode) #用来判断是否为unicode, is return true, not return false

Unicode (str, ' gb2312 ') is the same as Str.decode (' gb2312 '), which converts gb2312 encoded STR to Unicode encoding

Use str.__class__ to view the encoded form of STR

Principle said a half-day, the last to a package cure all ills it:)

#!/usr/bin/env python
#coding =utf-8
s= "Chinese"

If Isinstance (S, Unicode):
#s =u "Chinese"
Print S.encode (' gb2312 ')
Else
#s = "Chinese"
Print S.decode (' utf-8 '). Encode (' gb2312 ')

Voice Module Code:

#-*-coding:utf-8-*-importImportSYSPrint('Hello', Sys.getdefaultencoding ())defXfs_frame_info (words):#decode utf-8 to python internal Unicode codingisinstance (words,unicode) Wordu= Words.decode ('Utf-8')    #encode python Unicode to GBKdata = Wordu.encode ('GBK') Length= Len (data) + 2Frame_info= ByteArray (5) frame_info[0]= 0xfdframe_info[1] = (length >> 8) frame_info[2] = (Length & 0x00ff) frame_info[3] = 0x01frame_info[4] = 0x01buf= Frame_info +DataPrint("BUF:", BUF)returnbufif __name__=="__main__":    Print("Hello World") Words1= u'Hello'    #Encodetype = isinstance (words1,unicode)    #print ("Encodetype", Encodetype)    Print("Origin Unicode", words1) words= Words1.encode ('Utf-8')    Print("Utf-8 encoded", words) a=Xfs_frame_info (words)Print('a', a)if __name__=="__main__":    Print("Hello World") words1 ='Hello'    Print("oringe utf-8 encode:", WORDS1) Encodetype=isinstance (words1,unicode) Wordu = Words1.decode ('utf-8') Print("Unicode from Utf-8 decode:", Wordu)#Encodetype = isinstance (words1,utf-8)    #Encodetype = isinstance (words1, ' ASCII ')    #print ("Encodetype", Encodetype)    #print ("Origin Unicode", WORDS1)Word_utf8= Wordu.encode ('Utf-8')    #encodetype2 = isinstance (Words,utf8)    #print ("Encodetype2", Encodetype2)    Print("Utf-8 encoded", Word_utf8) a=xfs_frame_info (Word_utf8)Print('a'A

When you do not add the U ' ', one more step decode for Unicode

Python decode Unicode encode

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.