简体   繁体   English

使用utf-8字符串解码msgpack_numpy

[英]Decoding msgpack_numpy with utf-8 strings

I use python 3.6 with msgpack==0.5.1 and msgpack_numpy==0.4.2 . 我使用python 3.6和msgpack==0.5.1msgpack_numpy==0.4.2

When trying to encode and decode a dict , the string needs to be handled using utf-8 to restore the dict's keys as strings (instead of binaries). 尝试对dict进行编码和解码时, 需要使用utf-8处理字符串,以将dict的键恢复为字符串(而不是二进制文件)。

For example: 例如:

import msgpack

d = {'key': None}
binary = msgpack.packb(d)

ret = msgpack.unpackb(binary)
ret.keys()
>>> dict_keys([b'key'])

ret = msgpack.unpackb(binary, encoding='utf-8')
ret.keys()
>>> dict_keys(['key'])

However, when using msgpack_numpy , passing encoding='utf-8' brakes the numpy decoding: 但是,当使用msgpack_numpy ,传递encoding='utf-8' msgpack_numpy numpy解码:

import numpy as np
import msgpack_numpy as m
m.patch()

d['key'] = np.arange(5)
binary = msgpack.packb(d)

ret = msgpack.unpackb(binary)
ret.keys()
>>> dict_keys([b'key'])
ret[b'key']
>>> array([0, 1, 2, 3, 4])

ret = msgpack.unpackb(binary, encoding='utf-8')
ret.keys()
>>> dict_keys(['key'])
ret['key']
>>> {'data': '\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00', 'kind': '', 'nd': True, 'shape': [5], 'type': '<i8'}

Is it possible to encode/decode numpy arrays using msgpack without replacing the dict's keys to binary? 是否可以使用msgpack编码/解码numpy数组而无需将字典的键替换为二进制?

I fiddled with different packing options and discovered that using use_bin_type=True when packing the object solves the problem. 我弄弄了不同的打包选项,发现打包对象时使用use_bin_type=True解决了问题。

import msgpack
import numpy as np
import msgpack_numpy as m
m.patch()

d = {'key': np.arange(5)}
binary = msgpack.packb(d, use_bin_type=True)

ret = msgpack.unpackb(binary, encoding='utf-8')
ret.keys()
>>> dict_keys(['key'])
ret['key']
>>> array([0, 1, 2, 3, 4])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM