[英]python encode and decode with utf-16
>>>> enbytes = b'\xdf\x81\x9e\xbf"Q\xa37\xd0\x7f\x18\x1d:J\xe2\xa1'
>>>> enbytes.decode('utf-16').encode('utf-16')
b'\xff\xfe\xdf\x81\x9e\xbf"Q\xa37\xd0\x7f\x18\x1d:J\xe2\xa1'
為什么enbytes
不等於編碼字節。 如何解決。
那是字節順序標記(BOM),您可以只使用特定的字節序來防止添加 BOM,所以小字節序:
>>> enbytes.decode('utf-16').encode('utf-16-le')
b'\xdf\x81\x9e\xbf"Q\xa37\xd0\x7f\x18\x1d:J\xe2\xa1'
>>> enbytes == enbytes.decode('utf-16').encode('utf-16-le')
True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.