Skip to content

std/csv ​

CSV parsing and stringification.

milo
from "std/csv" import { Csv }

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.

Example ​

milo
let rows = Csv.parse(data)!
for row in rows {
    let name = row[0]
    let age = row[1]
    print(name + " is " + age)
}

API reference ​

Csv ​

milo
pub struct Csv

Stateless CSV operations grouped for discoverability.

Csv.parse ​

milo
fn Csv.parse(input: &string): Result<Vec<Vec<string>>>

Parse CSV text into rows of fields. Errs on an unterminated quoted field, a bare '"' inside an unquoted field, or text after a field's closing quote. Ragged row widths are accepted: the width a caller expects is schema policy, and a ragged file still parsed unambiguously.

A lone CR outside a quoted field is dropped, so CRLF files parse; inside a quoted field it is data and is kept.

Csv.stringify ​

milo
fn Csv.stringify(rows: &Vec<Vec<string>>): string

Serialize rows to CSV, quoting any field containing ',', '"', CR or LF.