Encoding conversion

Conversion encoding refers to encoding transformed into another code, such as utf-8 to gbk.

Why do I need transcoding it? Because different operating system code, utf-8 can not look directly at the win, because the windows are encoded by GBK, it was turned into gbk. If you turn the normal display with the GBK character on Linux \ Mac, you have to turn into utf-8 encoding.

 

Encoding and decoding

   s.encode ( "utf-8") # to utf-8 encoded into binary

   s.decode ( "utf-8") # decoded into utf-8 to unicode str

 

. 1 S = " Hello Future " 
2   
. 3 s_utf8 = s.encode ( " UTF-. 8 " )
 . 4   
. 5 s_utf8.decode ( " UTF-. 8 " )   # if not, then writing the brackets with the system default Utf- inside py3 8

 

Transcoding

The text conversion from one encoding into another, e.g. from Switch utf-8 gbk

gbk------》  unicode  》utf-8

unicode: Unicode, with among all the coding has mapped relationship

An original document is the win_data.txt gbk encoding, now converted to UTF- . 8 encoding
 2  
. 3 F = Open ( " win_data.txt " , " RB " )
 . 4  
. 5 S = reached, f.read ()
 . 6  
. 7  f.close ( )
 . 8  
. 9 s_unicode = s.decode ( " gbk " )   # the gbk decoded into unicode, converted to Unicode 
10  
. 11 s_utf8 = s_unicode.encode ( " UTF_8 " )
 12 is  
13 is F = Open ( " win_data.txt " , " WB ")
14 
15 f.write(s_utf8)
16 
17 f.close()

 

 

Guess you like

Origin www.cnblogs.com/jiajin-wu/p/11854216.html