msgpack反序列化dict密钥字符串转换为字节

msgpack反序列化dict密钥字符串转换为字节

本文介绍了msgpack反序列化dict密钥字符串转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中遇到msgpack的问题.似乎在序列化dict时,如果键是字符串str,则它们不会正确地反序列化并导致引发KeyError异常.

I am having issues with msgpack in python. It seems that when serialising a dict, if the keys are strings str, they are not unserialised properly and causing KeyError exceptions to be raised.

示例:

>>> import msgpack
>>> d = dict()
>>> value = 1234
>>> d['key'] = value
>>> binary = msgpack.dumps(d)
>>> new_d = msgpack.loads(binary)
>>> new_d['key']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'key'

这是因为键在调用loads()之后不是字符串,而是未序列化为bytes对象.

This is because the keys are not strings after calling loads() but are unserialised to bytes objects.

>>> d.keys()
dict_keys(['key'])
>>> new_d.keys()
dict_keys([b'key'])

似乎这与未实现的功能有关,如github中

It seems this is related to a unimplemented feature as mentioned in github

我的问题是,有没有办法解决此问题或解决办法,以确保反序列化时可以使用相同的密钥?

My question is, Is there a way to fix this issue or a work around to ensure that the same keys can be used upon deserialisation?

我想使用msgpack,但是如果我无法使用str键构建dict对象并且期望在反序列化时能够使用相同的键,则它将变得无用.

I would like to use msgpack but if I cannot build a dict object with str keys and expect to be able to use the same key upon deserilisation, it becomes useless.

推荐答案

调用dumpspackb

:param str encoding:
 |      Convert unicode to bytes with this encoding. (default: 'utf-8')

,但在调用loadsunpackb时默认未设置 ,如以下所示:

but it is not set by default when calling loads or unpackb as seen in:

Help on built-in function unpackb in module msgpack._unpacker:

unpackb(...)
    unpackb(... encoding=None, ... )

因此,更改反序列化的编码可解决此问题,例如:

Therefore changing the encoding on the deserialisation fixes the issue, for example:

>>> d['key'] = 1234
>>> binary = msgpack.dumps(d)
>>> msgpack.loads(binary, encoding = "utf-8")
{'key': 1234}
>>> msgpack.loads(binary, encoding = "utf-8") == d
True

这篇关于msgpack反序列化dict密钥字符串转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!