存储“对象”

发布于 2024-09-30 22:57:09 字数 243 浏览 0 评论 0原文

PyTables 支持存储 Python 对象吗? 像这样的事情:

dtype = np.dtype([('Name', '|S2'), ('objValue', object)])
data = np.zeros(3, dtype)
file.createArray(box3,'complicated',data)

当然,我在尝试执行此操作时遇到错误...... 如何正确存储对象数组?可能吗? 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞花火° 2024-10-07 22:57:09

您可以使用 Pytables 保存通用 Python 对象:

>>> 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')])

但是,这将在幕后使用 pickle (实际上是 cPickle),因此您将无法从其他语言访问这些对象(pickle 是仅 Python 本身支持的序列化格式) 。

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).

相思故 2024-10-07 22:57:09

如果您愿意,请尝试 pickle 模块将复杂的数据存储在相关库不支持的地方。

Try the pickle module if you want to store complicated data somewhere it isn't supported by the library in question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文