作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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
(只有在解包过程中才检测到错误,这不是很奇怪吗?)
最佳答案
这里有两个问题:msgpack
正在使用 strict_map_key=True
默认自版本 1.0.0 ( source ) 和 msgpack 的数组被隐式转换为 Python 的 lists
- 不可散列。为了使事情工作,传递所需的关键字参数:
with open(path, "rb") as f:
print(msgpack.unpackb(f.read(), use_list=False, strict_map_key=False))
# outputs: {(1, 2): 'str'}
关于python - 带有元组键的 msgpack 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66835419/
我是一名优秀的程序员,十分优秀!