如何从一个文件中解包多个msgpack数据

2024-09-24 02:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有可能从一个文件块中解包多个msgpack对象?你知道吗

在下面的示例代码中,我将2个msgpack对象写到了一个文件中,当我读回时,如果我指定了正确的大小,我可以毫无问题地解压它们。如果我完全读取文件,有没有办法将整个内容解压到反序列化对象列表中?你知道吗

Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 
>>> import msgpack
>>> d1 = {'a': 1, 'b': 2}
>>> a1 = [1,2,3,4,5]
>>> pack1 = msgpack.packb(d1, use_bin_type=True)
>>> pack2 = msgpack.packb(a1, use_bin_type=True)
>>> size1 = len(pack1)
>>> size2 = len(pack2)
>>> size1
7
>>> size2
6
>>> with open('test.dat', 'wb') as fh:
...     fh.write(pack1)
...     fh.write(pack2)
... 
7
6
>>> fh = open('test.dat', 'rb')
>>> p1 = fh.read(7)
>>> msgpack.unpackb(p1, raw=False)
{'a': 1, 'b': 2}
>>> fh.seek(7)
7
>>> p2 = fh.read(6)
>>> msgpack.unpackb(p2, raw=False, use_list=True)
[1, 2, 3, 4, 5]
>>> fh.seek(0)
0
>>> p = fh.read()
>>> p
b'\x82\xa1a\x01\xa1b\x02\x95\x01\x02\x03\x04\x05'
>>> msgpack.unpackb(p)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "msgpack/_unpacker.pyx", line 208, in msgpack._unpacker.unpackb
msgpack.exceptions.ExtraData: unpack(b) received extra data.
>>> for unp in msgpack.unpackb(p):
...     print(unp)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "msgpack/_unpacker.pyx", line 208, in msgpack._unpacker.unpackb
msgpack.exceptions.ExtraData: unpack(b) received extra data.

谢谢你抽出时间。你知道吗


Tags: 文件对象intrueforreaduseline
1条回答
网友
1楼 · 发布于 2024-09-24 02:23:07

感谢@hmm的评论,下面的代码可以工作:

>>> fh.seek(0)
0
>>> unp = msgpack.Unpacker(fh, raw=False)
>>> for unpacker in unp:
...     print(unpacker)
... 
{'a': 1, 'b': 2}
[1, 2, 3, 4, 5]

相关问题 更多 >