PGLearn provides utilities to save/load files in the HDF5 and JSON formats.

HDF5

To load HDF5 files, use the load_h5, which is the same as HDF5.h5read.

h = load_h5("test_file.h5", "/")  # load all HDF5 file

To save HDF5 files to disk, use the save_h5 function. All keys of the dictionary (and sub-dictionaries) must be Strings.

save_h5("myfile.h5", d)
Warning

When exporting data to an HDF5 file, only the following data types are supported:

Numerical data in an unsupported type will be converted to Float64, when possible.

JSON

Warning

Only .json extensions are supported

Use the load_json and save_json functions to load/save data to/from JSON files.

using PGLearn

# Load a dictionary from a JSON file
d = load_json("my_json_file.json")

# Save a dictionary to a JSON file
save_json("my_new_jsonfile.json", d)
save_json("my_pretty_jsonfile.json", d, indent=2)  # prettier formatting

Compressed JSON files (.json.gz and .json.bz2) are supported automatically

# Load a dictionary from a compressed JSON file
d = load_json("my_json_file.json.gz")
d = load_json("my_json_file.json.bz2")

# Save a dictionary to a compressed JSON file
save_json("my_new_jsonfile.json.gz", d)
save_json("my_new_jsonfile.json.bz2", d)