Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: Java Object Serialization Next: Working with Databases

Accessing HDF from Python using PyTables

Python offers a particularly convenient mechanism for accessing data in HDF files using the PyTables module:

from tables import *
# creating an object representing table rows
class Person(IsDescription):
name = StringCol(16) # 16 character string
age = Int32Col()
# opening a HDF5 file
h5file = open_file("people.h5", mode = "w", title = "personnel")
# definining a group in the HDF4 file
group = h5file.create_group("/", "records", "names and ages")
# defining a table in the group
table = h5file.create_table(group, "Personnel1",
Person, "Personnel Table 1")
Person = table.row
for ind in xrange(10):
Person['name'] = 'John Doe ' + str(ind)
Person['age'] = 20 + ind
for ind in xrange(10):
Person['name'] = 'Jane Smith ' + str(ind)
Person['age'] = 20 + ind
table.flush() # update disk
# getting a pointer to table
table = h5file.root.records.Personnel1
# running a query against file
arr = [x['name'] for x in table.iterrows() if
x['age'] > 25 and x['age'] < 28]
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content