python 3 的字串編碼預設為 utf-8.
如果不是 utf-8, 在 python 2 可以用 decode().encode() 來轉馬,但是 python 3 沒有 decode method 請教該怎麼做?
感謝指點!
[發問] 請教 python 3 的字串編碼問題
文章瀏覽次數 669 文章回覆數 1
留言
可以考慮先把字串 encode 成 byte
byte 就可以用 decode 回來
但我還不確定要怎麼證明 decode 回來的是 big5 string
參考看看
from chardet import detect
s = '許功蓋'
print(s, type(s))
s_byte = s.encode('big5')
print(s_byte, type(s_byte))
code = detect(s_byte)['encoding']
print(code)
s_big5 = s_byte.decode('big5')
print(s_big5, type(s_big5))