[Numpy-discussion] What to use to read and write numpy arrays to a file?
Mon Dec 8 14:56:40 CST 2008
The repr - eval pair does not work with numpy. You can simply do a
tofile() from file().
2008/12/8 Lou Pecora <firstname.lastname@example.org>:
> In looking for simple ways to read and write data (in a text readable format) to and from a file and later restoring the actual data when reading back in, I've found that numpy arrays don't seem to play well with repr and eval.
> E.g. to write some data (mixed types) to a file I can do this (fp is an open file),
> thedata=[3.0,-4.9+2.0j,'another string']
> repvars= repr(thedata)+"\n"
> Then to read it back and restore the data each to its original type,
> strvars= fp.readline()
> sonofdata= eval(strvars)
> which gives back the original data list.
> BUT when I try this with numpy arrays in the data list I find that repr of an array adds extra end-of-lines and that messes up the simple restoration of the data using eval.
> Am I missing something simple? I know I've seen people recommend ways to save arrays to files, but I'm wondering what is the most straight-forward? I really like the simple, pythonic approach of the repr - eval pairing.
> Thanks for any advice. (yes, I am googling, too)
> -- Lou Pecora, my views are my own.
> Numpy-discussion mailing list
Information System Engineer, Ph.D.
Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92
More information about the Numpy-discussion