-
Notifications
You must be signed in to change notification settings - Fork 18
pyio.parse_csv
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 |
| Type | Description |
|---|---|
{ { string* }* } |
A table that, for each row, contains a tables of strings |
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" } }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 and ASCII, there's a small risk of mis-identification.
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.