Python2.X UNICODE編碼解碼
阿新 • • 發佈:2019-01-25
</pre><pre code_snippet_id="1587825" snippet_file_name="blog_20160225_2_5984298" name="code" class="python">
## Python 2.X name2='層' # 檔案為UTF-8編碼 name2 # '\xe5\xb1\x82' print name2 # 層 print type(name2) # str name2.decode('UTF-8') # u'\u5c42' 從UTF-8解碼為UNICODE print type(name2.decode('UTF-8')) # UNICODE print name2.decode('UTF-8') # 層, UNICODE 列印為 UTF-8 name2.decode('UTF-8').encode('GBK') # '\xb2\xe3' UNICODE編碼為GBK print name2.decode('UTF-8').encode('GBK') # ²ã GBK 列印為 UTF-8, 亂碼 name2.decode('UTF-8').encode('GBK').decode('GBK') # u'\u5c42' 重新解碼為UNICODE print name2.decode('UTF-8').encode('GBK').decode('GBK') # 層 # UTF-8 name3='\xe5\xb1\x82' name3 # '\xe5\xb1\x82', UTF-8 print name3 # 層, UTF-8 列印為 UTF-8 print type(name3) # str name3.decode('UTF-8') # u'\u5c42' print name3.decode('UTF-8') # 層 # UNICODE name4='\u5c42' name4 # '\\u5c42' print name4 # \u5c42 name4.decode('unicode_escape') # u'\u5c42' print name4.decode('unicode_escape') # 層 # GBK name5='\xb2\xe3' name5 # '\xb2\xe3' name5.decode('GBK') # u'\u5c42' print name5.decode('GBK') # 層