Skip to content

csv

The csv module parses CSV text into structured rows or records, and serializes rows back to CSV. It is registered as a separate extension module; some hosts may need to opt into loading it explicitly.

use std::core::csv

csv::parse(text: string) -> Array<Array<string>>

Section titled “csv::parse(text: string) -> Array<Array<string>>”

Parse CSV text into an array of rows. Each row is an array of field strings.

let rows = csv::parse("name,age\nAlice,30\nBob,25")
// rows == [["name","age"], ["Alice","30"], ["Bob","25"]]
print(rows[1][0]) // "Alice"

csv::parse_records(text: string) -> Array<HashMap<string, string>>

Section titled “csv::parse_records(text: string) -> Array<HashMap<string, string>>”

Parse CSV text using the first row as headers; returns an array of records keyed by header name.

let recs = csv::parse_records("name,age\nAlice,30\nBob,25")
print(recs[0]["name"]) // "Alice"
print(recs[1]["age"]) // "25"

csv::stringify(data: Array<Array<string>>, delimiter: string) -> string

Section titled “csv::stringify(data: Array<Array<string>>, delimiter: string) -> string”

Convert an array of rows back to CSV text. delimiter is the field separator (typically ",").

let text = csv::stringify([["a","b"], ["1","2"]], ",")
print(text) // "a,b\n1,2\n"

csv::stringify_records(data, headers: Array<string>) -> string

Section titled “csv::stringify_records(data, headers: Array<string>) -> string”

Convert an array of HashMap records back to CSV with a header row. headers gives the explicit column order.

let recs = [{ name: "Alice", age: "30" }, { name: "Bob", age: "25" }]
let text = csv::stringify_records(recs, ["name", "age"])

csv::read_file(path: string) -> Result<Array<Array<string>>, string>

Section titled “csv::read_file(path: string) -> Result<Array<Array<string>>, string>”

Read and parse a CSV file.

match csv::read_file("data.csv") {
Ok(rows) => print(f"loaded {rows.length} rows"),
Err(e) => print(f"error: {e}"),
}

Quick validity check on CSV text.

csv::is_valid("a,b\n1,2") // true
  • json — structured data with typed deserialization
  • msgpack — binary serialization