Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Created May 18, 2012 19:17
Show Gist options
  • Save taldcroft/2727129 to your computer and use it in GitHub Desktop.
Save taldcroft/2727129 to your computer and use it in GitHub Desktop.
PyTables example
import tables
def make_h5_file(dat, filename, colname, n_rows):
"""Make a new HDF5 table to hold numpy array data ``dat``
"""
filters = tables.Filters(complevel=5, complib='zlib')
h5 = tables.openFile(filename, mode='w', filters=filters)
h5shape = (0,) + dat.shape[1:]
h5type = tables.Atom.from_dtype(dat.dtype)
h5.createEArray(h5.root, 'data', h5type, h5shape, title=colname,
expectedrows=n_rows)
h5.close()
def append_h5_file(dat, filename):
"""Append data to an existing HDF5 table
"""
h5 = tables.openFile(filename, mode='a')
h5.root.data.append(dat)
h5.close()
def read_h5_file(h5_slice, filename):
"""Read a slice of an HDF5 file"""
h5 = tables.openFile(filename)
dat = h5.root.data[h5_slice]
h5.close()
return dat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment