Skip to content

pyio.write_csv

Daniel Flassig edited this page Jul 7, 2026 · 5 revisions

Writes a table of rows to a CSV file.

pyio.write_csv(file, rows [, encoding])
Parameter Type Description
file path handle The file to write. The path handle must grant write access.
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
boolean true on success, false if the file could not be written (see remarks)

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" }
}

local folder = pyui.select_folder("full")
if folder then
    pyio.write_csv(folder:append_path("sample.csv"), rows)
end

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.

A writable path handle can be obtained from pyui.select_folder with "full" access. Passing a read-only handle raises an error. false is returned when the file cannot be opened or written.

Version Support:

Minimum PYTHA Version: V26

See also:

pyio, pyio.parse_csv, pyui.select_folder

Clone this wiki locally