Skip to content

pyio.parse_csv

Daniel Flassig edited this page May 6, 2025 · 17 revisions

Parses the fields of a CSV-file to tables of strings.

pyio.parse_csv(file) 
Parameter Type Description
file file handle The file that should be read

Return value

Type Description
{ { string* }* } A table that, for each row, contains a tables of strings

Example:

For example, a text file with the following content

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

would result in the following lua tables

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

Remarks:

The encoding of the text-file is automatically detected. UTF8, UTF8 with BOM, UTF16LE with BOM and ASCII are supported. If the encoding can be chosen freely, the either UTF8 with BOM or UTF16 with BOM are recommended. For UTF8 without BOM / ASCII, there's a small risk that the encoding might not be detected correctly.

For line endings, Windows and Unix line endings (CR+LF and LF) are equally supported.

The default field separator is the comma. Quoted fields are supported. Separators (commas) and newlines are allowed in quoted field. To include a literal quotation mark $"$, use two $""$. For details, see RFC4180.

See also:

pyio, pyio.parse_lines

Clone this wiki locally