我想知道是否可以从文件块中解压缩多个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.
谢谢您的时间。
答案 0 :(得分:1)
感谢@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]