I am new to PyTables and implemented a few basic techniques of inserting and retrieving data from a table in Pytables. However, I am not sure about how to insert data in an existing table of PyTables because all I read/get in the tutorial is creating a new table (using h5file.createTable()
method). Here is the basic code that tutorial has about inserting data into PytTables table created from scratch:
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()
P.S. There is one place in this tutorial that talks about appending data to an existing table, but uses the table that was created from scratch and basically gives no idea about selecting pre-existing table for appending data. Kindly help. Thanks.
You need open your file in append mode "a"
. Also do not create the group and table again. This appends another 10 rows:
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()