Skip to content

pyio.parse_csv

Daniel Flassig edited this page Jun 12, 2026 · 17 revisions

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

pyio.parse_csv(file) 
Parameter Type Description
file path 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, then one of the Unicode formats is recommended, preferably with BOM. For ASCII/Ansi there's a small risk that the automatic detection might still interpret the file as UTF-8.

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 fields. To include a literal quotation mark " in a quoted field, use two "". For details, see RFC4180.

Note that is some locales (e.g. Germany) some popular programs (e.g. Excel) will use semicolons instead of commas in the csv. It also uses a comma as a decimal separator in numbers, there (3,14). The function parse_csv() does not normalize these numbers automatically. Use string.gsub(field, ",", ".") to normalize these fields.

Version Support:

Minimum PYTHA Version: V26

See also:

pyio, pyio.parse_lines

Clone this wiki locally