首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python增量保存数据

使用python增量保存数据
EN

Stack Overflow用户
提问于 2018-07-04 06:29:58
回答 1查看 87关注 0票数 2

我正在做一个项目,其中有大量的数据正在生成。我想要一种方法来保存我的数据,这样我就不必将它们全部保存在RAM中。我目前正在使用numpy在程序结束时将所有内容保存在npz文件中。需要保存的内容包括标量、列表和列表列表。列表中的值是以增量方式添加的,因此我需要一种方法来追加到每个列表中,而不必将所有内容加载到内存中。

我对python还是个新手,所以如果有一个标准的方法可以做到这一点,请在那个方向给我指点一下。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-04 06:47:06

PyTables是一个numpy友好包,旨在将数据分页到磁盘,以便对内存不能容纳的数据集进行操作。

请参阅:https://www.pytables.org/usersguide/tutorials.html

https://kastnerkyle.github.io/posts/using-pytables-for-larger-than-ram-data-processing/

用法

# Create a data-frame description (called a table)
# each attribute of Particle below is a column.
from tables import *
class Particle(IsDescription):
    name      = StringCol(16)   # 16-character String
    idnumber  = Int64Col()      # Signed 64-bit integer
    ADCcount  = UInt16Col()     # Unsigned short integer
    TDCcount  = UInt8Col()      # unsigned byte
    grid_i    = Int32Col()      # 32-bit integer
    grid_j    = Int32Col()      # 32-bit integer
    pressure  = Float32Col()    # float  (single-precision)
    energy    = Float64Col()    # double (double-precision)

# create a hdf5 file on disk to store data in
h5file = open_file("tutorial1.h5", mode="w", title="Test file")

# create a table within the file, using the Particle description class
table = h5file.create_table(group, 'readout', Particle, "Readout example")

性能

它对于跨多个数据行的计算特别有用。

PyTables支持Blosc (这是一个巧妙的技巧)

您可以使用带where方法的blosc执行“内核内”查询。

result = [row['col2'] for row in table.where(
            '''(((col4 >= lim1) & (col4 < lim2)) |
               ((col2 > lim3) & (col2 < lim4)) &
               ((col1+3.1*col2+col3*col4) > lim5))''')]

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51163566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档