std/csv
CSV parsing and stringification.
from "std/csv" import { Csv }Functions
Csv.parse
fn Csv.parse(input: &string): Result<Vec<Vec<string>>>Parses a CSV string into a 2D vector of strings (rows of fields).
Strict about quoting, because every quoting mistake silently moves the field boundaries. An unterminated quoted field, a bare " inside an unquoted field, and text after a field's closing quote are each an Err naming the byte offset.
Ragged row widths are not an error. A ragged file still parsed unambiguously; the width a caller expects is schema policy, and this parser has no header or schema concept to check it against. Indexing past a short row is a bounds check, which fails loudly on its own.
A lone \r outside a quoted field is dropped, so CRLF files parse; inside a quoted field it is data and is kept.
let rows = Csv.parse(data)!
for row in rows {
let name = row[0]
let age = row[1]
print(name + " is " + age)
}Csv.stringify
fn Csv.stringify(rows: &Vec<Vec<string>>): stringSerializes a 2D vector of strings back into CSV format.