[Numpy-discussion] One Solution to: What to use to read and write numpy arrays to a file?
Tue Dec 9 10:50:14 CST 2008
I found one solution that's pretty simple for easy read and write to/from a file of a numpy array (see my original message below). Just use the method tolist().
e.g. a complex 2 x 2 array
Then use the repr - eval pairings to write and later read the list from the file and then convert the list that is read in back to an array:
arr_in=array(ls_in) # arr_in is same as arr
Seems to work well. Any comments?
-- Lou Pecora, my views are my own.
--- On Tue, 12/9/08, Lou Pecora wrote:
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),
Then to read it back and restore the data each to its original type,
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.
More information about the Numpy-discussion