SantoshGupta7
SantoshGupta7

Reputation: 6197

Is there way to write hdf5 files row by row in Python?

For CSV files we could use

writer = csv.writer(output)
writer.writerow([a, b, c, d])

Is there anything like that for writing Hdf5 files?

Upvotes: 2

Views: 1492

Answers (2)

SOG
SOG

Reputation: 912

If you are not bound to a specific technology, check out HDFql as this will alleviate you from low-level details when dealing with HDF5 files.

To solve your question, you need to create a dataset with two dimensions: the first is extendible and the second has a size of four (based on your code snippet, I assume you want to store four integers per row; also, if the datatype is not an integer, please check HDFql reference manual for an enumeration of all datatypes and change the code snippet below accordingly).

In Python, to create such dataset execute (called dset in this example):

HDFql.execute("CREATE DATASET dset AS INT(UNLIMITED, 4)")

Then, for each row you want to write, execute (please replace val0, val1, val2 and val3 with proper values):

HDFql.execute("INSERT INTO dset[-1:::] VALUES(%d, %d, %d, %d)" % (val0, val1, val2, val3))

... finally, extend the first dimension of dataset dset by one like this:

HDFql.execute("ALTER DIMENSION dset TO +1")

Repeat code snippet line #2 and #3 as many times as the rows you want to write.

Upvotes: 1

kcw78
kcw78

Reputation: 8046

This is similar to your other question.Error when trying to save hdf5 row where one column is a string and the other is an array of floats

With pytables, you can create an empty table referencing a dtype that defines each datataype (in this example, 4 floats). Then you use table_object.append(row_data) to add 1 or more rows of data, where row_data can be defined with a list of tuples or a numpy recarray. See complete example I posted to answer question above.

Upvotes: 1

Related Questions