Python examples¶
These workflows are covered by the project's tests.
Convert JSON to CSV¶
Simple file round-trip
from pytabify import DataTableCreator, DataTableSaver
datatable = DataTableCreator.from_file("people.json")
DataTableSaver.into_csv(datatable, "people.csv")
Expected behavior
This end-to-end flow is the simplest starting point for transforming a file quickly.
Load XLSX and save JSON¶
XLSX -> JSON
from pytabify import DataTableCreator, DataTableSaver
datatable = DataTableCreator.from_file("people.xlsx", sheet_name="People")
DataTableSaver.into_json(datatable, "people.json")
Missing sheet_name
XLSX reading raises an exception without a sheet name. Check the source workbook for the correct sheet.
Enrich records before saving¶
Expand schema and export
from pytabify import DataTableCreator, DataTableSaver
datatable = DataTableCreator.from_records(
[
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
]
)
datatable[0]["country"] = "MX"
DataTableSaver.into_json(datatable, "people-enriched.json")
Use in tests¶
Fixtures and assertions
from pytabify import DataTableCreator
datatable = DataTableCreator.from_records(
[
{"name": "Alice", "active": True, "nickname": None},
{"name": "Bob", "active": False, "nickname": None},
]
)
assert datatable[0].active is True
assert datatable[1].nickname is None
Choose the format
For assertions depending on native types, use from_records, JSON or XLSX. CSV reading returns strings.