Numpy Array 到 base64 并返回到 Numpy Array - Python [英] Numpy Array to base64 and back to Numpy Array - Python

查看:52
本文介绍了Numpy Array 到 base64 并返回到 Numpy Array - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在想弄清楚如何从 base64 数据中恢复一个 numpy 数组.这个问题和答案表明这是可能的:在 Python 之外读取 numpy 数组 但是没有给出一个例子.

以下面的代码为例,如果知道数组的dtype和shape,如何从base64数据中得到一个Numpy数组?

导入base64将 numpy 导入为 npt = np.arange(25, dtype=np.float64)s = base64.b64encode(t)r = base64.decodestring(s)q = ?????

我想要一个 python 语句将 q 设置为 dtype float64 的 numpy 数组,因此结果是一个与 t 相同的数组.这是编码和解码的数组的样子:

<预><代码>>>>t = np.arange(25,dtype=np.float64)>>>吨数组([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.,11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21.,22., 23., 24.])>>>s=base64.b64encode(t)>>>秒'AAAAAAAAAAAAAAAAAADwPwAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAAAAAAAAAAAAGEAAAAAAAAAcQAAAAAAAACBAAAAAAAAAIkAAAAAAAAAkQAAAAAAAACZAAAAAAAAAAKEAAAAAAAAAqQAAAAAAAAAwQAAAAAAAAAADFAAAAAAAAAAAMkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>>>r = base64.decodestring(s)>>>r'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x0\x00\x00\x18@\x00\x00\x00\x00\x00\x00\x1c@\x00\x00\x00\x00\x00\x00 @\x00\x00\x00\x00\x00\x00"@\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00&@\x00\x00\x00\x00\x00\x00(@\x00\x00\x00\x00\x00\x00*@\x00\x00\x00\x00\x00\x00,@\x00\x00\x00\x00\x00\x00.@\x00\x00\x00\x00\x00\x000@\x00\x00\x00\x00\x00\x001@\x00\x00\x00\x00\x00\x002@\x00\x00\x00\x00\x00\x003@\x00\x00\x00\x00\x00\x004@\x00\x00\x00\x00\x00\x005@\x00\x00\x00\x00\x00\x006@\x00\x00\x00\x00\x00\x007@\x00\x00\x00\x00\x00\x00\x00\x00'>>>q = np.array( ??

我问这个问题的原因是因为我正在从事一个项目,我想在一个由 django 提供支持的应用程序的 MySQL 数据库中存储大量 Numpy 数组.

使用这个 django 片段,我可以在文本字段中存储 base64 数据:http://djangosnippets.org/snippets/1669/

我想将数组以 base64 格式写入数据库,而不是将数组转换为 unicode 字符串.

感谢您的帮助.

解决方案

import base64将 numpy 导入为 npt = np.arange(25, dtype=np.float64)s = base64.b64encode(t)r = base64.decodebytes(s)q = np.frombuffer(r, dtype=np.float64)打印(np.allclose(q,t))# 真的

I am now trying to figure out how I can recover a numpy array from base64 data. This question and answer suggest it is possible: Reading numpy arrays outside of Python but an example is not given.

Using the code below as an example, how can I get a Numpy array from the base64 data if I know the dtype and the shape of the array?

import base64
import numpy as np

t = np.arange(25, dtype=np.float64)
s = base64.b64encode(t)
r = base64.decodestring(s)
q = ????? 

I want a python statement to set q as a numpy array of dtype float64 so the result is an array identical to t. This is what the arrays encoded and decoded look like:

>>> t = np.arange(25,dtype=np.float64)
>>> t
array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,
    11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,
    22.,  23.,  24.])
>>> s=base64.b64encode(t)
>>> s
'AAAAAAAAAAAAAAAAAADwPwAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABRAAAAAAAAAGEAAAAAAAAAcQAAAAAAAACBAAAAAAAAAIkAAAAAAAAAkQAAAAAAAACZAAAAAAAAAKEAAAAAAAAAqQAAAAAAAACxAAAAAAAAALkAAAAAAAAAwQAAAAAAAADFAAAAAAAAAMkAAAAAAAAAzQAAAAAAAADRAAAAAAAAANUAAAAAAAAA2QAAAAAAAADdAAAAAAAAAOEA='
>>> r = base64.decodestring(s)
>>> r
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x18@\x00\x00\x00\x00\x00\x00\x1c@\x00\x00\x00\x00\x00\x00 @\x00\x00\x00\x00\x00\x00"@\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00&@\x00\x00\x00\x00\x00\x00(@\x00\x00\x00\x00\x00\x00*@\x00\x00\x00\x00\x00\x00,@\x00\x00\x00\x00\x00\x00.@\x00\x00\x00\x00\x00\x000@\x00\x00\x00\x00\x00\x001@\x00\x00\x00\x00\x00\x002@\x00\x00\x00\x00\x00\x003@\x00\x00\x00\x00\x00\x004@\x00\x00\x00\x00\x00\x005@\x00\x00\x00\x00\x00\x006@\x00\x00\x00\x00\x00\x007@\x00\x00\x00\x00\x00\x008@'
>>> q = np.array( ????

The reason I am asking is because I am working on a project where I would like to store a lot of Numpy arrays in a MySQL database in an app powered by django.

Using this django snippet I can store base64 data in a textfield: http://djangosnippets.org/snippets/1669/

I want to write the arrays to the database as base64 instead of converting the arrays to a string of unicode.

Thanks for your help.

解决方案

import base64
import numpy as np

t = np.arange(25, dtype=np.float64)
s = base64.b64encode(t)
r = base64.decodebytes(s)
q = np.frombuffer(r, dtype=np.float64)

print(np.allclose(q, t))
# True

这篇关于Numpy Array 到 base64 并返回到 Numpy Array - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆