无法解码 utf-16 字符串
Trouble decoding utf-16 string
我正在使用 python3.3
。我一直在尝试解码某个看起来像这样的字符串:
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xed:\xf9w\xdaH\xd2?\xcf\xbc....
继续进行。但是,每当我尝试使用 str.decode('utf-16')
解码此字符串时,我都会收到一条错误消息:
'utf16' codec can't decode bytes in position 54-55: illegal UTF-16 surrogate
我不太确定如何解码这个字符串。
gzip 数据 begins with \x1f\x8b\x08
so my guess is that your data is gzipped. Try gunzipping the data 解码前。
import io
import gzip
# this raises IOError because `buf` is incomplete. It may work if you supply the complete buf
buf = b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xed:\xf9w\xdaH\xd2?\xcf\xbc'
with gzip.GzipFile(fileobj=io.BytesIO(buf)) as f:
content = f.read()
print(content.decode('utf-16'))
我正在使用 python3.3
。我一直在尝试解码某个看起来像这样的字符串:
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xed:\xf9w\xdaH\xd2?\xcf\xbc....
继续进行。但是,每当我尝试使用 str.decode('utf-16')
解码此字符串时,我都会收到一条错误消息:
'utf16' codec can't decode bytes in position 54-55: illegal UTF-16 surrogate
我不太确定如何解码这个字符串。
gzip 数据 begins with \x1f\x8b\x08
so my guess is that your data is gzipped. Try gunzipping the data 解码前。
import io
import gzip
# this raises IOError because `buf` is incomplete. It may work if you supply the complete buf
buf = b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xed:\xf9w\xdaH\xd2?\xcf\xbc'
with gzip.GzipFile(fileobj=io.BytesIO(buf)) as f:
content = f.read()
print(content.decode('utf-16'))