[英]Numpy Array to base64 and back to Numpy Array - Python
I am now trying to figure out how I can recover a numpy array from base64 data.我现在想弄清楚如何从 base64 数据中恢复一个 numpy 数组。 This question and answer suggest it is possible: Reading numpy arrays outside of Python but an example is not given.
这个问题和答案表明这是可能的: Reading numpy arrays outside of Python但没有给出一个例子。
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?以下面的代码为例,如果我知道数组的 dtype 和形状,如何从 base64 数据中获取 Numpy 数组?
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.我想要一个 python 语句将 q 设置为 dtype float64 的 numpy 数组,因此结果是一个与 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.我问的原因是因为我正在从事一个项目,我想在一个由 django 提供支持的应用程序的 MySQL 数据库中存储大量 Numpy 数组。
Using this django snippet I can store base64 data in a textfield: http://djangosnippets.org/snippets/1669/使用这个 django 片段,我可以将 base64 数据存储在文本字段中: 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.我想将数组作为 base64 写入数据库,而不是将数组转换为 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
The code below will encode it as base64.下面的代码将其编码为 base64。 It will handle numpy arrays of any type/size without needing to remember what it was.
它将处理任何类型/大小的 numpy 数组,而无需记住它是什么。 It will also handle other arbitrary objects that can be pickled.
它还将处理其他可以腌制的任意对象。
import numpy as np
import pickle
import codecs
obj = np.random.normal(size=(10, 10))
obj_base64string = codecs.encode(pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL), "base64").decode('latin1')
obj_reconstituted = pickle.loads(codecs.decode(obj_base64string.encode('latin1'), "base64"))
You can remove .decode('latin1') and .encode('latin1') if you just want the raw bytes.如果您只想要原始字节,您可以删除 .decode('latin1') 和 .encode('latin1') 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.