-
Notifications
You must be signed in to change notification settings - Fork 18
pyio.write_csv
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 row is a table of strings (fields) |
encoding |
string (optional)
|
Encoding of the output file ("utf8", "utf8-bom", "utf16le", "utf16be", "ascii") |
| Type | Description |
|---|---|
nil |
No return value |
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"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.