The task is to save not only the data buffer, but also the form and type. np.fromstring reads the data buffer, but as a 1d array; you should get dtype and shape from another place.
In [184]: a=np.arange(12).reshape(3,4) In [185]: np.fromstring(a.tostring(),int) Out[185]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) In [186]: np.fromstring(a.tostring(),a.dtype).reshape(a.shape) Out[186]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
The Python object persistence mechanism, executed in time, is pickle , and numpy conforms to socket requirements:
In [169]: import pickle In [170]: a=np.arange(12).reshape(3,4) In [171]: s=pickle.dumps(a*2) In [172]: s Out[172]: "cnumpy.core.multiarray\n_reconstruct\np0\n(cnumpy\nndarray\np1\n(I0\ntp2\nS'b'\np3\ntp4\nRp5\n(I1\n(I3\nI4\ntp6\ncnumpy\ndtype\np7\n(S'i4'\np8\nI0\nI1\ntp9\nRp10\n(I3\nS'<'\np11\nNNNI-1\nI-1\nI0\ntp12\nbI00\nS'\\x00\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\n\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x0e\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x12\\x00\\x00\\x00\\x14\\x00\\x00\\x00\\x16\\x00\\x00\\x00'\np13\ntp14\nb." In [173]: pickle.loads(s) Out[173]: array([[ 0, 2, 4, 6], [ 8, 10, 12, 14], [16, 18, 20, 22]])
There is a numpy function here that can read the brine line:
In [181]: np.loads(s) Out[181]: array([[ 0, 2, 4, 6], [ 8, 10, 12, 14], [16, 18, 20, 22]])
You pointed np.save to the string, but you cannot use np.load . The way around this is a step further into the code and use np.lib.npyio.format .
In [174]: import StringIO In [175]: S=StringIO.StringIO()
The save line has a header with dtype and shape info:
In [179]: S.seek(0) In [180]: S.readlines() Out[180]: ["\x93NUMPY\x01\x00F\x00{'descr': '<f8', 'fortran_order': False, 'shape': (3, 4), } \n", '\x00\x00\x00\x00\x00\x00\x00\x00ffffff\n', '@ffffff\ x1a@ \xcc\xcc\xcc\xcc\xcc\xcc#@ffffff*@\x00\x00\x00\x00\x00\ x800@ \xcc\xcc\xcc\xcc\xcc\ xcc3@ \x99\x99\x99\x99\x99\ x197@ffffff :@33333\ xb3=@ \x00\x00\x00\x00\x00\ x80@ @fffff& B@ ']
If you need a user readable string, you can try json .
In [196]: import json In [197]: js=json.dumps(a.tolist()) In [198]: js Out[198]: '[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]' In [199]: np.array(json.loads(js)) Out[199]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
Going to / from an array list view is the most obvious use of json . Maybe someone wrote a more complex representation of json arrays.
You can also switch to the csv format - there were a lot of questions about reading / writing csv arrays.
'[[ 0.5544 0.4456], [ 0.8811 0.1189]]'
is a bad string representation for this purpose. It is very similar to str() array, but with , instead of \n . But there is no clean way to parse nested [] , and the missing delimiter is a pain. If it consistently uses , then json can convert it to a list.
np.matrix takes a string of type MATLAB:
In [207]: np.matrix(' 0.5544, 0.4456;0.8811, 0.1189') Out[207]: matrix([[ 0.5544, 0.4456], [ 0.8811, 0.1189]]) In [208]: str(np.matrix(' 0.5544, 0.4456;0.8811, 0.1189')) Out[208]: '[[ 0.5544 0.4456]\n [ 0.8811 0.1189]]'