"Character encoding and transcoding"
Why Encode and Transcode
Because each country computer's character encoding format is not unified (column China: GBK), the same software put to different countries of the computer will appear garbled, this situation how to solve it?! Of course, because all the computers in all countries support Unicode, then we can put Unicode as a springboard, first convert the character encoding to Unicode, in the conversion of Unicode to another country's character encoding (example Korea), there will be no garbled situation. Of course this is just a transcoding set not translated into Korean don't confuse.
Python3.0 for encoding Conversion
#__author__: "Shengxin"#__DATE__:2017/9/9#Python3.0 EnvironmentImportSys,chardetPrint(Sys.getdefaultencoding ())#Get default encoding (UTF-8)Name ="shengxin" #The name is Unicode encoded at this timeName_utf8= Name.encode ("Utf-8")#Convert to UTF-8 encodingPrint(Name_utf8)Print(Chardet.detect (Name_utf8))#Note: Unicode encoding cannot view encoding format with ChardetPrint(Name_utf8.decode ("Utf-8"))#transcode to Unicode after encoding to UTF-8NAME_GBK= Name.encode ("GBK")#Convert to GBK encodingPrint(NAME_GBK)Print(Chardet.detect (NAME_GBK))#View the current character encoding formatPrint(Name_gbk.decode ("GBK"))#transcode to Unicode after encoding to GBK #Utf-8#b ' \xe8\xbf\x9e\xe5\xbf\x97\xe9\x9b\xb7 '#{' confidence ': 0.87625, ' encoding ': ' Utf-8 '}#shengxin#b ' \xc1\xac\xd6\xbe\xc0\xd7 '#{' confidence ': 0.73, ' encoding ': ' windows-1252 '}#shengxin
Encoding conversion in the Python2.0
① declaration character encoding (UTF-8)
#__author__: "Shengxin"#__DATE__:2017/9/9 #Python2.0 Environment default encoding ASCIIImportsysname="Hello" #there is no character "Hello" in the ASCII code, the name is Uft-8 Print(Sys.getdefaultencoding ())#get the default encodingPrint(Name.decode ("Utf-8"))#decoding the Uft-8 code to UnicodeName_unicode=name.decode ("Utf-8")Print(Name_unicode,type (Name.decode ("Utf-8")))#View the current character encodingNAME_GBK=name_unicode.encode ("GBK")#converts a character to Unicode to GBKPrint(NAME_GBK)#ASCII#Hello#(U ' \u4f60\u597d ', <type ' Unicode ' >)#???
② using the default character encoding (ASCII)
#__author__: "Shengxin"#__DATE__:2017/9/9Importsysname="Nihao" #English characters, and the second line character declaration is removed, the name is the ASCII code at the moment Print(Sys.getdefaultencoding ())#Get System EncodingName_unicode = Name.decode ("ASCII")#ASCII conversion to UnicodePrint(Name_unicode,type (name_unicode)) Name_utf8=name_unicode.encode ("Utf-8")#convert Unicode to Utf-8Print(Name_utf8,type (Name_utf8)) NAME_GBK=name_unicode.encode ("GBK")#convert Unicode to GBKPrint(Name_gbk,type (NAME_GBK))#ASCII#(U ' nihao ', <type ' Unicode ' >)#(' Nihao ', <type ' str ' >)#(' Nihao ', <type ' str ' >)
Summary: The default character encoding in python2.x is ASCII, if the encoding format is not declared, the input character formatting is ASCII code (Chinese is not in ASCII, input error); If you want to enter Chinese characters, you need to declare the encoding format, when the encoding format of Chinese characters do not follow the default character encoding format, but with the declaration of the The code format is consistent (the above instance is Utf-8), which should be remembered.
〖python〗--Encoding Conversion