我有一个包含一些数据的文本文件,在这些数据中,有一个带有msgPack的JSON。
我可以在https://toolslick.com/conversion/data/messagepack-to-json上解压,但无法在python中运行。
到目前为止,我正在尝试执行以下操作:
def parseAndSplit(path):
with open(path) as f:
fContent = f.read()
for subf in fContent.split('Payload: '):
'''for ssubf in subf.split('DataChunkMsg'):
print(ssubf)'''
return subf.split('DataChunkMsg')[0]
fpath = "path/to/file"
t = parseAndSplit(fpath)
l = t.split("-")
s = ""
for i in l:
s=s+i
print(s)
a = msgpack.unpackb(bytes(s,"UTF-8"), raw=False)
print(a)
但输出是
import msgpack
Traceback (most recent call last):
File "C:/Users/Marco/PycharmProjects/codeTest/msgPack.py", line 19, in <module>
a = msgpack.unpackb(bytes(s,"UTF-8"), raw=False)
File "msgpack\_unpacker.pyx", line 202, in msgpack._cmsgpack.unpackb
msgpack.exceptions.ExtraData: unpack(b) received extra data.
9392AA6E722D736230322D3032AC4F444D44617...(string goes on)
我非常确定这是某种编码问题,但是无论在文档中还是尝试使用,我都没有运气。
非常感谢您的关注
我最终找到了解决方案:
msgpack.unpackb(bytes.fromhex(hexstring)
其中hexstring是从文件读取的字符串。