Python 中文編碼

# -*- coding:utf-8 -*-
# ASCII 是一種單字節的編碼,可表示256個不一樣字符
# 中文 在 python3 中默認用 unicode編碼
lst = ['你',        # str類型,unicode編碼
    str('你'), # 同上
    u'你', # 同上
    '你'.encode('utf-8').decode('utf-8'), # 同上
    # encode 將 str 轉爲 bytes 類型,能夠再用 decode 轉回 str 類型
    
    '你'.encode('utf-8'), # b'\xe4\xbd\xa0',utf-8編碼,一個漢字 3 Byte
    '你'.encode('gbk'), # b'\xc4\xe3',gbk、gbxxxx 編碼,一個漢字 2 Byte
    '你'.encode('GB2312') # 同上
    ]

for word in lst:
    print (word, type(word))
相關文章
相關標籤/搜索