Python jsonpickle 的数组问题

Python Array Issue with jsonpickle

我这里有一些价值观。打印数组可以很好地显示值。 然而,执行 Jsonpickle 会显示一些关于 numpy 和 py/tuple 项目的信息。不知道为什么,并试图清除缓存并重建。那并没有解决问题。任何人都知道为什么会发生这种情况?我的所有其他阵列都可以使用 jsonpickle 正常打印。

看来我指的是一些内存而不是值。

print(test)
[104.97287576678693, 99.802264802045, 115.59585412670378, 103.42889046311696, 116.20011484134734]


print(jsonpickle.encode(test))
[{"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/reduce": [{"py/type": "numpy.dtype"}, {"py/tuple": ["f8", false, true]}, {"py/tuple": [3, "<", null, null, null, -1, -1, 0]}]}, {"py/b64": "7lq4mEM+WkA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "+eB3TljzWEA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "SNhYeSLmXEA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "1Sn88HLbWUA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "/cF6rs4MXUA="}]}]}]

错误:

jsonpickle.encode(list(np.array([1, 2, 3, 4])))

'[{"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/reduce": [{"py/type": "numpy.dtype"}, {"py/tuple": ["i8", 0, 1]}, {"py/tuple": [3, "<", null, null, null, -1, -1, 0]}]}, {"py/b64": "AQAAAAAAAAA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "AgAAAAAAAAA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "AwAAAAAAAAA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "BAAAAAAAAAA="}]}]}]'

右:

jsonpickle.encode(np.array([1, 2, 3, 4]).tolist())
'[1, 2, 3, 4]'

编码 numpy 数组时,确保您必须通过注册其处理程序来启用 numpy 扩展:

>>> import jsonpickle.ext.numpy as jsonpickle_numpy
>>> jsonpickle_numpy.register_handlers()

之后,编码应该可以工作了