Skip to content

Instantly share code, notes, and snippets.

@martjanz
Created March 1, 2018 23:38
Show Gist options
  • Select an option

  • Save martjanz/853a3825f602ee8b6746740f2abd4741 to your computer and use it in GitHub Desktop.

Select an option

Save martjanz/853a3825f602ee8b6746740f2abd4741 to your computer and use it in GitHub Desktop.
Export HDF5 tables to CSV files
"""Export HDF5 tables to csv files
"""
import argparse
import os
import pandas as pd
parser = argparse.ArgumentParser(description='Export HDF5 tables to csv files.')
parser.add_argument('-o', '--h5', dest='h5_path', required=True,
help='Path to HDF5 file.')
parser.add_argument('-d', '--csv', dest='csv_path', default='./',
help='Path where csv files will be stored (default: current path)')
# Parse arguments
args = parser.parse_args()
# Check if HDF5 file exists
if not os.path.isfile(args.h5_path):
print 'ERROR: hdf5 file does not exists.'
exit(-1)
# Open HDF5 file
store = pd.HDFStore(args.h5_path)
tables = store.keys()
# Check if destination path exists
if not os.path.isdir(args.csv_path):
print 'ERROR: destination path does not exists.'
exit(-1)
# Store tables as csv filesa
for table in tables:
table_name = table.replace('/', '')
store[table].to_csv(os.path.join(args.csv_path, table_name + '.csv'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment