storing 'object'

雨燕双飞 提交于 2019-12-08 07:52:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!