I am working with Python 2.
I have saved a dict
of arrays
to a .npy
file on my computer. If I open it as a text file, a just see a mess of ASCII characters, as one would expect since I am not just saving arrays.
I can see its contents by np.load
it in a Python console, but I was wondering whether there is a GUI that allows me to see the contents of the file directly, i.e. without going through a python console?
Basically: If I went back, I would save it as a json
or hdf5
file so that I can open them with some viewer and could clearly see the data.
Having not done that, and having saved .npy
files, can I see its contents in a similar way if they were json or hdf5?
Advertisement
Answer
np.load
(or a clone) is the only why to load a npy
:
In [84]: adict = {'a':np.arange(3), 'b':np.ones((2,3),int)} In [85]: np.save('foo.npy',adict) In [86]: np.load('foo.npy') Out[86]: array({'a': array([0, 1, 2]), 'b': array([[1, 1, 1], [1, 1, 1]])}, dtype=object)
Note that the dictionary has been wrapped in an 0d object dtype array. The dictionary structure was then pickled
. And arrays with the dictionary were pickled with np.save
format. Both a dictionary and an object dtype array can contain pointers to other Python objects, which need their own pickling method. In other words, it’s Python all the way down!.
If you’d attempted to use json
you’d have found that numpy arrays are not json serializable. You would have had to apply tolist()
to turn the arrays into lists. json
just saves dictionaries, lists and strings (basic javascript structures that many languages share). There may also be a third party serializer to numpy arrays.
h5
is written in C++, with lots of language interfaces. h5dump
is a handy viewer. But to write the dictionary from your Python dictionary you’d have to use h5py
(or pytables
). A dictionary maps nicely onto h5 groups, and numpy arrays to h5 datasets.