import msgpack
path = 'test.msgpack'
with open(path, "wb") as outfile:
outfile.write(msgpack.packb({ (1,2): 'str' }))很好,现在
with open(path, 'rb') as infile:
print(msgpack.unpackb(infile.read()))错误与
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "msgpack/_unpacker.pyx", line 195, in msgpack._cmsgpack.unpackb
ValueError: list is not allowed for map key(这难道不是完全奇怪的错误,只有在打开包装时才能检测到吗?)
如何编写或解决使用元组作为键的python?
发布于 2021-03-27 20:55:21
这里有两个问题:msgpack默认使用strict_map_key=True,因为版本1.0.0 (来源)和msgpack的数组被隐式转换为的lists --这是不可接受的。要使工作正常,传递所需的关键字参数:
with open(path, "rb") as f:
print(msgpack.unpackb(f.read(), use_list=False, strict_map_key=False))
# outputs: {(1, 2): 'str'}https://stackoverflow.com/questions/66835419
复制相似问题