Skip to content

Instantly share code, notes, and snippets.

@canismarko
Created March 2, 2023 15:06
Show Gist options
  • Save canismarko/5e6f2b92a626933b19ad0cc009cdf7b1 to your computer and use it in GitHub Desktop.
Save canismarko/5e6f2b92a626933b19ad0cc009cdf7b1 to your computer and use it in GitHub Desktop.
Converting HDF to TXT files
import pandas as pd
import h5py
from pathlib import Path
from tqdm import tqdm
target_dir = Path("./")
for fp in tqdm(list(target_dir.iterdir())):
if fp.suffix != ".hdf":
continue
# Load the file and convert to a pandas dataframe
with h5py.File(fp, mode='r') as h5fd:
try:
data_grp = h5fd['entry/data']
except KeyError:
print("Skipped bad file:", str(fp))
continue
keys = list(data_grp.keys())
data = {key: data_grp[key][()] for key in keys}
df = pd.DataFrame(data)
# Save file to CSV
csv_fp = f"{fp.stem}.csv"
df.to_csv(csv_fp, sep="\t")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment