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

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, table_path)
    emzed.io.save_csv(peaks, csv_path)
    emzed.io.save_excel(peaks, xlsx_path)

    loaded = emzed.io.load_table(table_path)

    print(f"saved files in {folder}")
    print(f"loaded {len(loaded)} rows from {table_path.name}")
    print(loaded)
saved files in /var/folders/k8/zfp7dvcs1m326gz1brql1tv80000gn/T/tmp49m1l987
loaded 2 rows from peaks.table
compound  mz           rt
str       MzType       RtType
--------  -----------  --------
caffeine   195.087650    0.70 m
glucose    181.070660    1.00 m