Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据附加到现有的pytables表_Python_Pytables - Fatal编程技术网

Python 将数据附加到现有的pytables表

Python 将数据附加到现有的pytables表,python,pytables,Python,Pytables,我是PyTables新手,在PyTables中实现了一些从表中插入和检索数据的基本技术。但是,我不确定如何在现有的PyTables表中插入数据,因为我在中读取/获取的只是创建一个新表(使用h5file.createTable()方法)。以下是教程中关于将数据插入从头创建的PytTables表的基本代码: h5file = openFile("tutorial1.h5", mode = "w", title = "Test file") group = h5file.createGroup("/"

我是PyTables新手,在PyTables中实现了一些从表中插入和检索数据的基本技术。但是,我不确定如何在现有的PyTables表中插入数据,因为我在中读取/获取的只是创建一个新表(使用
h5file.createTable()
方法)。以下是教程中关于将数据插入从头创建的PytTables表的基本代码:

h5file = openFile("tutorial1.h5", mode = "w", title = "Test file")
group = h5file.createGroup("/", 'detector', 'Detector information')
table = h5file.createTable(group, 'readout', Particle, "Readout example")

for i in xrange(10):
    particle['name']  = 'Particle: %6d' % (i)
    particle['TDCcount'] = i % 256
    particle['ADCcount'] = (i * 256) % (1 << 16)
    particle['grid_i'] = i
    particle['grid_j'] = 10 - i
    particle['pressure'] = float(i*i)
    particle['energy'] = float(particle['pressure'] ** 4)
    particle['idnumber'] = i * (2 ** 34)
    # Insert a new particle record
    particle.append()

    table.flush()
h5file=openFile(“tutorial1.h5”,mode=“w”,title=“测试文件”)
group=h5file.createGroup(“/”、“检测器”、“检测器信息”)
table=h5file.createTable(组“读出”,粒子,“读出示例”)
对于X范围内的i(10):
粒子['name']='粒子:%6d'(i)
粒子['tAccount']=i%256

粒子['ADCcount']=(i*256)%(1您需要在附加模式下打开文件
“a”
。也不要再次创建组和表。这将附加另外10行:

import tables


class Particle(tables.IsDescription):
    name      = tables.StringCol(16)   # 16-character String
    idnumber  = tables.Int64Col()      # Signed 64-bit integer
    ADCcount  = tables.UInt16Col()     # Unsigned short integer
    TDCcount  = tables.UInt8Col()      # unsigned byte
    grid_i    = tables.Int32Col()      # 32-bit integer
    grid_j    = tables.Int32Col()      # 32-bit integer
    pressure  = tables.Float32Col()    # float  (single-precision)
    energy    = tables.Float64Col()    # double (double-precision)

h5file = tables.openFile("tutorial1.h5", mode = "a")
table = h5file.root.detector.readout

particle = table.row

for i in range(10, 20):
    particle['name']  = 'Particle: %6d' % (i)
    particle['TDCcount'] = i % 256
    particle['ADCcount'] = (i * 256) % (1 << 16)
    particle['grid_i'] = i
    particle['grid_j'] = 10 - i
    particle['pressure'] = float(i*i)
    particle['energy'] = float(particle['pressure'] ** 4)
    particle['idnumber'] = i * (2 ** 34)
    # Insert a new particle record
    particle.append()

h5file.close()
导入表
类粒子(表.说明):
name=tables.StringCol(16)#16个字符串
idnumber=tables.Int64Col()#有符号64位整数
ADCcount=tables.UInt16Col()#无符号短整数
TDCcount=tables.UInt8Col()#无符号字节
grid_i=tables.Int32Col()#32位整数
grid_j=tables.Int32Col()#32位整数
压力=表。浮动32COL()#浮动(单精度)
能量=表格。Float64Col()#双精度(双精度)
h5file=tables.openFile(“tutorial1.h5”,mode=“a”)
表=h5file.root.detector.readout
particle=table.row
对于范围(10,20)内的i:
粒子['name']='粒子:%6d'(i)
粒子['tAccount']=i%256

particle['ADCcount']=(i*256)%(1@khan此解决方案成功了吗?谢谢@Mike,它在Python 3.5上非常适合我(使用
range
而不是
xrange
),应该标记为答案。如果缺少组和表,我稍微修改了示例以创建它们: