Skip to content

I/O Basics

The emzed.io helpers load and save common emzed data types. If a path is omitted and emzed-gui is installed, these helpers can fall back to file dialogs; beginner scripts should pass paths explicitly.

"""Save and load emzed tables in a temporary folder."""

from pathlib import Path
from tempfile import TemporaryDirectory

import emzed


def main():
    peaks = emzed.Table.create_table(
        ["compound", "mz", "rt"],
        [str, emzed.MzType, emzed.RtType],
        rows=[
            ["caffeine", 195.08765, 42.0],
            ["glucose", 181.07066, 60.0],
        ],
    )

    with TemporaryDirectory() as tmp:
        folder = Path(tmp)
        table_path = folder / "peaks.table"
        csv_path = folder / "peaks.csv"
        xlsx_path = folder / "peaks.xlsx"

        emzed.io.save_table(peaks, str(table_path))
        emzed.io.save_csv(peaks, str(csv_path))
        emzed.io.save_excel(peaks, str(xlsx_path))

        loaded = emzed.io.load_table(str(table_path))

        print(f"saved files in {folder}")
        print(f"loaded {len(loaded)} rows from {table_path.name}")
        print(loaded)


if __name__ == "__main__":
    main()
saved files in /tmp/tmpal8k3p_c
loaded 2 rows from peaks.table
compound  mz           rt      
str       MzType       RtType  
--------  -----------  --------
caffeine   195.087650    0.70 m
glucose    181.070660    1.00 m