python - 如何从一个文件 block 中解压许多 msgpack 数据

标签 python msgpack

我想知道是否可以从一个文件 block 中解压多个 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.

感谢您的宝贵时间。

最佳答案

感谢@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]

关于python - 如何从一个文件 block 中解压许多 msgpack 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58307286/

相关文章:

.net - 使用 ServiceStack JsonSerializer 反序列化包含 Dictionary 属性的类型

c++ - 使用 msgpack 对用户定义的结构进行编码。

python - Django Rest Framework 如何禁止用户更改用户名?

python - 如何从 Pandas DF 中删除特殊字符?

python - 如何管理错误 : TypeError: argument must be an int, 或使用 fileno() 方法

c++ - 使用 MsgPack 通过 ZeroMQ (zmqpp) 发送数据给出 'msgpack::v1::insufficient_bytes' 错误

c++ - 在 msgpack 的 C++ 实现中,如何在使用 pack_map 或 pack_array 序列化对象后反序列化对象?

javascript - 如何从 Javascript 调用 MessagePack RPC 服务?

python - Flask系统找不到指定的路径: '130.127.5.9'

python - Django:GET css 返回 404?