0

I can store ndarray in a text file like this :

>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> np.savetxt('a.out',a,'%d')

then file a.out looks like this :

1 2 3
4 5 6

now, I can load it back :

>>> b = np.loadtxt('a.out','int')
>>> b
array([[1, 2, 3],
       [4, 5, 6]])

so far so good for (2, 3) array.
Now I want to do the same for (1, 2, 3) array.

>>> a = np.array([[[1,2,3],[4,5,6]],[[3,2,1],[6,5,4]]])
>>> a
array([[[1, 2, 3],
        [4, 5, 6]],

       [[3, 2, 1],
        [6, 5, 4]]])
>>> np.savetxt('a.out',a,'%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ckim/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 1160, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('int64') and format specifier ('%d %d')

What is the correct way to store the array? and how can I load it back? I read the document but it is not well explaining about how to specify the format for a complex ndarray.

I tried (not document for '%s', '%sf', int32, int4, ..)

np.savetxt('a.out',a,'%s')

and it gives me a.out file

[1 2 3] [4 5 6]
[3 2 1] [6 5 4]

that's more like it, but I don't know how to load it. I tried below to no avail.

>>> b = np.loadtxt('a.out','%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ckim/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 873, in loadtxt
    dtype = np.dtype(dtype)
TypeError: data type "%d" not understood
>>> b = np.loadtxt('a.out','%s')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ckim/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 873, in loadtxt
    dtype = np.dtype(dtype)
TypeError: data type "%s" not understood

Please anybody help me with this. Thanks!

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • Is there a reason you want to save it as an ascii file? Would a binary dump be acceptible? If so, `np.save` or `np.savez` would be more appropriate ... – mgilson Jul 27 '16 at 06:24
  • @mgilson, ok your link(duplicate) is very helpful. and thanks also for save and savez. (I didn't know it exists). I prefer human readable data (some possibilities to handle it with C/C++ program) – Chan Kim Jul 27 '16 at 06:35
  • Fair enough. As noted in the dupe question, `savetxt` doesn't really handle arrays of ndim > 2. You could use [ndarray.tofile](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tofile.html). As long as your C++ work is on a similar system (endianness and all that jazz -- which is fairly standard these days anyway), you shouldn't have too much problem reading it... – mgilson Jul 27 '16 at 06:39
  • 1
    Even if you saved it with `C/C++` in mind you'd have to decide how to represent 3d as rows and columns. `savetxt` writes a `csv` which is common enough. There isn't a 3d standard. The code for `savetxt` is easy to read - just iterate on the array and write each row as a formatted line. – hpaulj Jul 27 '16 at 06:59

0 Answers0