Skip to content

Instantly share code, notes, and snippets.

@Markus-de-Koster
Created August 14, 2024 09:33
Show Gist options
  • Select an option

  • Save Markus-de-Koster/21865bfe766a28a32ff72b8f907c53f5 to your computer and use it in GitHub Desktop.

Select an option

Save Markus-de-Koster/21865bfe766a28a32ff72b8f907c53f5 to your computer and use it in GitHub Desktop.
Quick example for h5 to excel / csv conversion
import pandas as pd
import h5py
import numpy as np
import os
# h5 example file
h5_file_path = 'test_file.h5'
def h5_to_excel_or_csv(h5_file, output_file=None, output_format='csv'):
"""
Reads an HDF5 file, displays its metadata, and converts it to an Excel or CSV file.
Parameters:
h5_file (str): Path to the HDF5 file.
output_file (str): Path to the output file (without extension). If None, the same name as the HDF5 file will be used.
output_format (str): 'csv' or 'excel' for the output format.
"""
# Display metadata
with h5py.File(h5_file, 'r') as h5f:
print("Metadata:")
for key, value in h5f['/data'].attrs.items():
print(f"{key}: {value}")
# Read the data
df = pd.read_hdf(h5_file, key="data")
# Determine output file name
if output_file is None:
output_file = os.path.splitext(h5_file)[0]
# Convert to CSV or Excel
if output_format.lower() == 'csv':
df.to_csv(f"{output_file}.csv", index=False)
print(f"Data converted to CSV: {output_file}.csv")
elif output_format.lower() == 'excel':
df.to_excel(f"{output_file}.xlsx", index=False)
print(f"Data converted to Excel: {output_file}.xlsx")
else:
raise ValueError("Unsupported output format. Choose 'csv' or 'excel'.")
if __name__ == '__main__':
h5_to_excel_or_csv(h5_file_path, output_format='excel')
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment