std.csv
CSV, the tables as text that spreadsheets and databases write and read: a line for each row, the values separated by commas, between quotes when they need it (RFC 4180).
import std.csv
list<list<string>> rows = csv.parse("name,city\n\"Rossi, Anna\",Rome\n")
println("{}", rows[1][0]) // Rossi, Anna
csv.Table people = csv.table(fs.readText("people.csv"))
for (i64 i = 0; i < people.len(); i++) {
println("{} lives in {}", people.get(i, "name"), people.get(i, "city"))
}
fs.writeText("out.csv", csv.encode([["id", "note"], ["7", "said \"hello\""]]))The guide: JSON.
Generata dalla documentazione nei sorgenti della libreria, che è in inglese.
#Tipi
#Table
struct TableA table with a name for each column: the first row of a CSV, and the rows after it. It is made by table and tableWith, or by a program, which fills names and rows.
list<string> names: of the columns, in their orderlist<list<string>> rows: the values, a list for each row
#Table.len
i64 len() constHow many rows there are, without the one of the names.
#Table.column
i64 column(string name) constThe place of the column name, from 0, or -1 when there is none.
#Table.get
string get(i64 row, string name) constThe value of the column name in the row row, from 0; "" for a row that is shorter than its names. Throws an Error for a column that is not there; a row out of the table is a panic, as an index out of a list.
#Table.values
list<string> values(string name) constThe values of the column name, one for each row. Throws an Error for a column that is not there.
#Table.text
string text() constThe table as a text of CSV, its names in the first line.
#Table.textWith
string textWith(u8 separator) constThe table as a text with separator between the values.
#Funzioni
#parse
list<list<string>> parse(string text)The rows of a text of CSV, each the list of its values, with a comma between the values. A value between double quotes can have commas, ends of line and quotes, each quote written twice. The lines end with "\n" or "\r\n"; the empty lines are passed over, and the rows can have different numbers of values. Throws an Error with the line for quotes that are not closed, or with something after them.
#parseWith
list<list<string>> parseWith(string text, u8 separator)The rows of a text whose values are separated by separator: ; for the files of the spreadsheets where the comma is of the decimals, as in Italy, \t for the values separated by tabs.
#encode
string encode(list<list<string>> rows)The rows as a text of CSV, a line ended by "\n" for each, with a comma between the values. A value with a comma, a quote, an end of line or a space at its ends goes between double quotes, its quotes written twice: parse gives the same rows back.
#encodeWith
string encodeWith(list<list<string>> rows, u8 separator)The rows as a text with separator between the values, as encode.
#table
Table table(string text)The table of a text of CSV whose first row has the names of the columns. A text without rows gives a table without names.
#tableWith
Table tableWith(string text, u8 separator)The table of a text whose values are separated by separator.