詳細文章:html
http://www.cnblogs.com/yuanchenqi/articles/5956943.htmlpython
http://www.diveintopython3.net/strings.htmlide
|
上圖僅適用於py2code
先說python2htm
再說python3blog
#-*-coding:utf-8-*- import sys print(sys.getdefaultencoding()) #ascii msg = "你好" msg_gbk = msg.decode("utf-8").encode("gbk") msg_gbk_to_utf8 = msg_gbk.decode("gbk").encode("utf-8") print(msg) print(msg_gbk) print(msg_gbk_to_utf8)
import sys print(sys.getdefaultencoding()) #utf-8 msg="你好" msg_gbk = msg.encode("gbk") #默認就是Unicode,不用再decode msg_gbk_to_unicode = msg_gbk.decode("gbk") msg_gbk_to_utf8 = msg_gbk.decode("gbk").encode("utf-8") print(msg) #你好 print(msg_gbk) #b'\xc4\xe3\xba\xc3' print(msg_gbk_to_unicode) #你好 print(msg_gbk_to_utf8) #b'\xe4\xbd\xa0\xe5\xa5\xbd'
編碼應用比較多的場景應該是爬蟲了,互聯網上不少網站用的編碼格式很雜,雖然總體趨向都變成utf-8,但如今仍是很雜,因此爬網頁時就須要你進行各類編碼的轉換,不過生活正在變美好,期待一個不須要轉碼的世界。