[英]Decoding utf-8 in python
我有一個這樣的表達式,它產生 utf-8 表示的字節列表。
list(chr(number).encode("utf-8"))
但是如何反過來呢?
說,我有 2 個字節 [292, 200] 作為列表,如何將它們解碼為符號?
您可以在 0..255 范圍內的整數列表中調用bytes
。
因此,您的示例反轉如下:
>>> bytes([195, 136]).decode('utf8')
'È'
如果您想要代碼點,請將其包裝在ord()
中:
>>> ord(bytes([195, 136]).decode('utf8'))
200
注意:僅當字節序列對應於單個 Unicode 字符(代碼點)時,最后一步才有效。
您必須記住,char 只存儲 8 位:-128 到 127。因此,如果“數字”大於 char 限制,它將不起作用。
number = 127 print(f"number: {number}") li = list(chr(number).encode("utf-8")) print(f"List of byte: {li}") dec = int.from_bytes(li, byteorder='big') print(f"Type dec: {type(dec)}") print(f"Value dec: {dec}")
number = 128 print(f"number: {number}") li = list(chr(number).encode("utf-8")) print(f"List of byte: {li}") dec = int.from_bytes(li, byteorder='big') print(f"Type dec: {type(dec)}") print(f"Value dec: {dec}")
查看python 文檔以轉換值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.