I have a large data set (millions of rows) in memory, in the form of numpy arrays and dictionaries .
After creating this data, I want to store it in files; so later I can quickly load these files into memory without reloading this data from scratch.
np.save and np.load do the job smoothly for numpy arrays.
But I am facing problems with dict objects.
See the example below. d2 is the dictionary that was loaded from the file. See #out [28], it was loaded into d2 as a numpy array, not as a dict. . So further operations with dict, such as get, do not work.
Is there a way to load data from a file as a dict (instead of a numpy array)?
In [25]: d1={'key1':[5,10], 'key2':[50,100]} In [26]: np.save("d1.npy", d1) In [27]: d2=np.load("d1.npy") In [28]: d2 Out[28]: array({'key2': [50, 100], 'key1': [5, 10]}, dtype=object) In [30]: d1.get('key1') #original dict before saving into file Out[30]: [5, 10] In [31]: d2.get('key2') #dictionary loaded from the file --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-31-23e02e45bf22> in <module>() ----> 1 d2.get('key2') AttributeError: 'numpy.ndarray' object has no attribute 'get'
source share