Skip to content

CSV and TSV

Rahmad Afandi edited this page Jun 7, 2026 · 1 revision

CSV and TSV

Output format is chosen from the file extension: .csv → CSV, .tsv → tab-separated.

from rustpy_xlsxwriter import FastExcel, write_csv

# Builder
FastExcel("out.csv").sheet("ignored", records).save()
FastExcel("out.tsv").sheet("ignored", records).save()

# Functional
write_csv(records, "out.csv")
write_csv(records, "out.tsv", delimiter="\t")

CSV/TSV output supports a single sheet — adding more than one before saving to a CSV target raises ValueError.

The same inputs work as for Excel: list/generator of dicts, pandas, polars, Arrow. The Arrow path is zero-copy here too.

Escaping

Fields are escaped per RFC 4180: a value containing a comma, quote, CR, or LF is wrapped in double quotes and internal quotes are doubled.

Formula-injection guard

A CSV cell beginning with =, +, -, or @ can be executed as a formula when the file is opened in a spreadsheet app — the classic CSV injection vector. RFC-4180 escaping does not stop this.

Opt in to neutralize it: cells starting with those characters get a leading ' so they're treated as text.

FastExcel("safe.csv", sanitize_formulas=True).sheet("S", records).save()
write_csv(records, "safe.csv", sanitize_formulas=True)

It is off by default to keep output byte-identical for existing callers, and has no effect on .xlsx output (Excel cells are already written as typed text, not formulas).

Float / int formatting

CSV numbers use ryu (floats) and itoa (ints) for fast, round-trip-correct formatting. Datetimes are emitted as ISO 8601 (YYYY-MM-DDTHH:MM:SS).

Clone this wiki locally