Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

django - 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. 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
'x00x00x00x00x00x00x00x00x00x00x00x00x00x00xf0?x00x00x00x00x00x00x00@x00x00x00x00x00x00x08@x00x00x00x00x00x00x10@x00x00x00x00x00x00x14@x00x00x00x00x00x00x18@x00x00x00x00x00x00x1c@x00x00x00x00x00x00 @x00x00x00x00x00x00"@x00x00x00x00x00x00$@x00x00x00x00x00x00&@x00x00x00x00x00x00(@x00x00x00x00x00x00*@x00x00x00x00x00x00,@x00x00x00x00x00x00.@x00x00x00x00x00x000@x00x00x00x00x00x001@x00x00x00x00x00x002@x00x00x00x00x00x003@x00x00x00x00x00x004@x00x00x00x00x00x005@x00x00x00x00x00x006@x00x00x00x00x00x007@x00x00x00x00x00x008@'
>>> 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...