Recipe - csv2hdf5

How to convert a table of values with columns seperated by commas/tabs to hdf5.

Given the following example text file:

v ge gi
0.1 0.2 0.3
0.11 0.22 0.33
0.111 0.222 0.333

Assume it is stored locally as "data.txt"

import tables
import pylab

# load the text file, skipping the "labels" row
d = pylab.load('data.txt',skiprows=1)

# get column names from first line
column_names = [x.strip() for x in file('data.txt').readline().split(' ')]

# open new file in write mode
h5 = tables.openFile('data.h5','w')

# write columns

for i,name in enumerate(column_names):
    h5.createArray('/',name,d[:,i])

h5.close()

# read it back

h5 = tables.openFile('data.h5')

ge = h5.root.ge.read()
gi = h5.root.gi.read()
v = h5.root.v.read()

h5.close()