cromo La guida di Cromo
Scarica PDF EN

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).

cromo
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

cromo
struct Table

A 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 order
  • list<list<string>> rows: the values, a list for each row

#Table.len

cromo
i64 len() const

How many rows there are, without the one of the names.

#Table.column

cromo
i64 column(string name) const

The place of the column name, from 0, or -1 when there is none.

#Table.get

cromo
string get(i64 row, string name) const

The 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

cromo
list<string> values(string name) const

The values of the column name, one for each row. Throws an Error for a column that is not there.

#Table.text

cromo
string text() const

The table as a text of CSV, its names in the first line.

#Table.textWith

cromo
string textWith(u8 separator) const

The table as a text with separator between the values.

#Funzioni

#parse

cromo
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

cromo
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

cromo
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

cromo
string encodeWith(list<list<string>> rows, u8 separator)

The rows as a text with separator between the values, as encode.

#table

cromo
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

cromo
Table tableWith(string text, u8 separator)

The table of a text whose values are separated by separator.