问题
Does PyTables support storing Python objects? something like this :
dtype = np.dtype([('Name', '|S2'), ('objValue', object)])
data = np.zeros(3, dtype)
file.createArray(box3,'complicated',data)
I get error when trying to do this of course... How to properly store arrays of objects?Is it possible? Thanks
回答1:
Try the pickle module if you want to store complicated data somewhere it isn't supported by the library in question.
回答2:
You can save generic Python object with Pytables:
>>> dtype = np.dtype([('Name', '|S2'), ('objValue', object)])
>>> data = np.zeros(3, dtype)
>>> file = tables.openFile('/tmp/test.h5', 'w')
>>> myobjects = file.createVLArray(file.root, 'myobjects', tables.ObjectAtom())
>>> myobjects.append(data)
>>> myobjects[0]
array([('', 0), ('', 0), ('', 0)],
dtype=[('Name', '|S2'), ('objValue', '|O8')])
However, this will use pickle (cPickle in fact) behind the scenes, so you won't be able to access these objects from other languages (pickle is a serialization format only supported by Python itself).
来源:https://stackoverflow.com/questions/4104812/storing-object