Module std::encoding::csv

CSV parsing โ€” pure Hew implementation.

Parse CSV data from strings or files into a table that can be queried by row/column index or column name.

Examples

import std.encoding.csv;

fn main() {
    match csv.parse("name,age\nAlice,30\nBob,25") {
        .Ok(table) => println(table.get_by_name(0, "name")), // "Alice"
        .Err(error) => println(error),
    }
}

Contents

Functions

Function parse

pub fn parse(data: string) -> Result<Table, string>

Parse CSV data from a string (first row is treated as headers).

Returns Err(string) if the input is not well-formed CSV. A malformed quote state is a failure, not a repairable shape: an unterminated quoted field otherwise parses as a complete table, which is exactly what a truncated file must not look like.

Function parse_no_headers

pub fn parse_no_headers(data: string) -> Result<Table, string>

Parse CSV data from a string without treating the first row as headers.

Returns Err(string) if the input is not well-formed CSV.

Function parse_file

pub fn parse_file(path: string) -> Result<Table, string>

Parse CSV data from a file path (first row is treated as headers).

Returns Err(string) if the file cannot be read or the input is not well-formed CSV.

Traits

Trait TableMethods

Methods available on a CSV Table.

Methods

fn rows(self: Self) -> i64

Return the number of data rows (excluding the header row).

fn cols(self: Self) -> i64

Return the number of columns.

fn header(self: Self, col: i64) -> string

Return the header name for the given column index.

fn get(self: Self, row: i64, col: i64) -> string

Return the cell value at the given row and column index.

fn get_by_name(self: Self, row: i64, col_name: string) -> string

Return the cell value at the given row for the named column.