Csv
Csv converts between CSV text, as RFC 4180 describes it, and rows of fields.
Details
- Every field is a
string, both ways. Convert numbers yourself, with String.toInt after parsing and interpolation before writing. - A header row is an ordinary row:
Csv.parsereturns it first, and neither function checks that rows have the same number of fields. - Malformed text is an
Errfrom Csv.parse, naming the line. A separator that is not one code point other than a quote,\r,\n, U+0000, and U+FFFD panics in both functions, since the separator is part of the program, not the data. - Csv works on text in memory. Read and write files with File.read and File.write.
Basic examples
Parse CSV text into rows of fields with Csv.parse:
Csv.parse("name,age\nAnn,31\nBob,27") // Ok{ value: [["name", "age"], ["Ann", "31"], ["Bob", "27"]] }Write rows as CSV text with Csv.stringify. Each row ends with a line break:
Csv.stringify([["name", "age"], ["Ann", "31"]]) // "name,age\nAnn,31\n"A field that holds the separator, a quote, or a line break, or begins with white space, is quoted on the way out and unquoted on the way in:
let rows = [["city", "note"], ["Lima, Peru", "said \"hi\""]]
print(Csv.stringify(rows))
println(Csv.parse(Csv.stringify(rows)))city,note
"Lima, Peru","said ""hi"""
Ok{ value: [["city", "note"], ["Lima, Peru", "said \"hi\""]] }Scope
Both functions take sep:, for semicolon- or tab-separated text:
Csv.parse("name;price\nTea;1,50", sep: ";") // Ok{ value: [["name", "price"], ["Tea", "1,50"]] }
Csv.stringify([["id", "name"], ["7", "Ann"]], sep: "\t") // "id\tname\n7\tAnn\n"Rows may have different numbers of fields, and empty fields stay "":
Csv.parse("a,b,c\n1\n2,,") // Ok{ value: [["a", "b", "c"], ["1"], ["2", "", ""]] }Csv.parse reads \n and \r\n line endings, and drops empty lines. Csv.stringify writes \n:
Csv.parse("a,b\r\n\r\nc,d\r\n") // Ok{ value: [["a", "b"], ["c", "d"]] }No text gives no rows, and no rows give no text:
Csv.parse("") // Ok{ value: [] }
Csv.stringify([]) // ""A field with leading white space is quoted, and a row of one empty field is written "", so Csv.parse keeps it:
Csv.stringify([[" x", "y "], [""]]) // "\" x\",y \n\"\"\n"Functions by job
- Csv.parse: the rows of CSV text, or an
Errnaming the line of a stray or unclosed quote - Csv.stringify: CSV text for rows, a line per row, quoting fields that need it
Applications
Read a file, skip its header, and total a column for each group:
func totals(rows: Array[Array[string]]): Result[Map[string, int], Error] {
let sums: Map[string, int] = Map{}
for row in rows.slice(1, rows.length()) {
let amount = row.get(1)?.toInt()
if amount == null {
return Result.fail("bad row ${row}")
}
sums[row[0]] = (sums.get(row[0]) ?? 0) + amount
}
Result.ok(sums)
}
let text = try File.read("sales.csv")
let rows = try (Csv.parse(text) | Result.wrapErr("sales.csv"))
println(try totals(rows))Map{ "north": 165, "south": 80 }Convert tab-separated text to CSV:
let tsv = "id\tcity\n1\tLima, Peru\n2\tOslo\n"
let rows = try Csv.parse(tsv, sep: "\t")
print(Csv.stringify(rows))id,city
1,"Lima, Peru"
2,OsloWrite records as CSV with a header row:
type Item = { name: string, qty: int }
let items = [Item{ name: "apple", qty: 3 }, Item{ name: "fig, dried", qty: 12 }]
let rows = [["name", "qty"]].concat(items.map({ [x.name, "${x.qty}"] }))
print(Csv.stringify(rows))name,qty
apple,3
"fig, dried",12Properties and relations
Csv.parse reads back the rows Csv.stringify writes, with the same separator, as long as no row is empty:
let rows = [["a", "b, c"], ["two\nlines", " x"], [""]]
assert(Result.unwrap(Csv.parse(Csv.stringify(rows))) == rows)
assert(Result.unwrap(Csv.parse(Csv.stringify(rows, sep: ";"), sep: ";")) == rows)Possible issues
Fields stay strings, so numbers compare as text until you convert them:
let row = Result.unwrap(Csv.parse("9,10"))[0]
row[0] < row[1] // false
(row[0].toInt() ?? 0) < (row[1].toInt() ?? 0) // trueA quote inside an unquoted field is an Err, not a panic:
Csv.parse("id,note\n1,say \"hi\"") // Err{ error: Error{ message: "parse error on line 2, column 7: bare \" in non-quoted-field" } }A separator of more than one code point panics:
Csv.parse("a, b", sep: ", ") // panic: Csv.parse needs a separator of one character other than a quote, a line break, U+0000 or U+FFFD, got ", "A row with no fields is written as an empty line, which Csv.parse drops:
Csv.parse(Csv.stringify([["a"], [], ["b"]])) // Ok{ value: [["a"], ["b"]] }See also
Csv.parse, Csv.stringify, Json, File.read, Result.wrapErr, String.toInt
