SunnyEdward
2022-03-11 11:07
采纳率: 75%
浏览 50
已结题

关于msgpack报错: ExtraData: unpack(b) received extra data.

问题遇到的现象和发生背景

我有多个msgpack文件,把他们组合在一起后,就打不开了。

组合多个msgpack
import msgpack
with open('/home/suned/data/giscup_2021_1/totalmsgpack/total.msgpack','wb') as f1:
    for i in range(1,32):
        file_name = f"202008{i:02}.msgpack"
        with open(f'/home/suned/data/giscup_2021_1/msgpack/{file_name}','rb') as f2:
            unpacked = msgpack.unpackb(f2.read())
            msgpack.dump(unpacked,f1)
想要反序列化组合后的文件(total.msgpack)
import msgpack
with open('/home/suned/data/giscup_2021_1/totalmsgpack/total.msgpack', 'rb') as f1:
    dct1 = msgpack.unpackb(f1.read())
    print(dct1)

报错

ExtraData                                 Traceback (most recent call last)
/tmp/ipykernel_85730/2692480189.py in <module>
      1 import msgpack
      2 with open('/home/suned/data/giscup_2021_1/totalmsgpack/total.msgpack', 'rb') as f1:
----> 3     dct1 = msgpack.unpackb(f1.read())
      4     print(dct1)

msgpack/_unpacker.pyx in msgpack._cmsgpack.unpackb()

ExtraData: unpack(b) received extra data.
但是组合前的单个msgpack是可以打开的,比如打开20200801.msgpack
with open('/home/suned/data/giscup_2021_1/msgpack/20200801.msgpack', 'rb') as f2:
    dct2 = msgpack.unpackb(f2.read())
    print(dct2)

img

我的想法

单个msgpack做unpackb后的结构是一个列表,列表里面包含多个字典结构,类似
[{'order_id': 5966280.0, 'eta': 531.0, 'dist': 2923.0, 'simple_eta': 495.0, 'driver_id': 75457.0, 'slice_id': 153.0},{'order_id': 6099942.0, 'eta': 590.0, 'dist': 3212.278, 'simple_eta': 559.0, 'driver_id': 19210.0, 'slice_id': 157.0}...]
是不是做了组合后,totalmsgpack里面包含了多个列表结构,所以出现了这个错误呢?(received extra data)
弄了一天了,没能解决呀,头疼。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题