[Numpy-discussion] Saving and loading a structured array from a TEXT file

Emmanuel Mayssat emayssat at gmail.com
Mon Jan 23 15:15:56 EST 2012


Is there a way to save a structured array in a text file?
My problem is not so much in the saving procedure, but rather in the
'reloading' procedure.
See below


In [3]: import numpy as np

In [4]: r = np.ones(3,dtype=[('name', '|S5'), ('foo', '<i8'), ('bar', '<f8')])

In [5]: r.tofile('toto.txt',sep='\n')

bash-4.2$ cat toto.txt
('1', 1, 1.0)
('1', 1, 1.0)
('1', 1, 1.0)

In [7]: r2 = np.fromfile('toto.txt',sep='\n',dtype=r.dtype)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/cls1fs/clseng/10/<ipython-input-7-b07ba265ede7> in <module>()
----> 1 r2 = np.fromfile('toto.txt',sep='\n',dtype=r.dtype)

ValueError: Unable to read character files of that array type


--
Emmanuel



More information about the NumPy-Discussion mailing list