Skip to content

pyio.write_csv

fabian-flassig edited this page Mar 18, 2026 · 5 revisions

Writes a table of rows to a CSV file.

pyio.write_csv(path, rows [, encoding])
Parameter Type Description
path string The file path where the CSV should be written
rows { { string* }* } A table of rows, each line is a table of strings (fields)
encoding string (optional) Encoding of the output file ("utf8", "utf8-bom", "utf16le", "utf16be", "ascii")

Return value

Type Description
nil No return value

Example:

Writing the following Lua table

local rows = {
    { "Author", "Poem", "Year" },
    { "Lord Byron", "She walks in beauty", "1814" },
    { "Friedrich Schiller", "Freude, schöner Götterfunken" }
}

pyio.write_csv("sample.csv", rows)

will create a CSV file with the following content:

Author,Poem,Year
Lord Byron,She walks in beauty,1814
Friedrich Schiller,"Freude, schöner Götterfunken"

Remarks:

Each inner table represents one row. Fields within a row are separated by commas.

Fields are automatically quoted if required. This happens when a field contains commas, quotation marks, or line breaks. Quotation marks inside fields are escaped by doubling them ("").

Rows are written line by line. For UTF-16 encodings, Windows-style line endings (CR+LF) are used. For UTF-8 and ASCII, line endings are written as LF.

Supported encodings include UTF-8 (with or without BOM), UTF-16 (little endian and big endian), and ASCII. If no encoding is specified, UTF-8 is used by default.

The function does not perform locale-specific conversions (e.g. decimal commas). Values are written exactly as provided.

Version Support:

Minimum PYTHA Version: V26

See also:

pyio, pyio.parse_csv

Clone this wiki locally