packages

table::csv

On this page

CSV (RFC 4180) through the csv package: a header line, any one-byte delimiter, "-quoted fields with "" for a quote and line breaks inside, LF / CRLF / CR line ends, a UTF-8 BOM skipped. Columns are typed by trying, in order, bool, i64, f64, date, datetime and str on their non-null cells (or as types says). CsvReader reads an io::Read in chunks and yields tables of up to batch rows, so memory stays flat for large files.

Structs

CsvSource

pub struct Csv

How to read or write CSV. Start from Csv::new() and change what differs.

nabi
use table::{Csv, DType};
let o = Csv::new().delimiter(';').null("-").types([("id", DType::Str)]);
let t = try o.read_str("id;x\n007;1,5\n008;-\n") catch _ { table::Table::new() };
assert(t.row(0).str("id") == "007");
assert(t.column_at(1).null_count() == 1);
  • pub delim: u8§

    Source

    The field separator, , by default.

  • pub header: bool§

    Source

    The first line names the columns (else column_1, ...).

  • pub nulls: List<str>§

    Source

    Cells read as null; written for a null.

  • pub types: Dict<str, DType>§

    Source

    Column types that skip inference.

  • pub infer_rows: usize§

    Source

    Rows looked at to pick types when streaming (CsvReader).

  • pub batch: usize§

    Source

    Rows per CsvReader::next table.

  • pub crlf: bool§

    Source

    Write \r\n line ends (RFC 4180); \n by default.

  • pub trim: bool§

    Source

    Strip whitespace around unquoted fields.

  • pub fn new() -> Csv§

    Source

    Comma-separated, with a header; "", NA, N/A, null, NULL read as null.

  • pub fn delimiter(self, c: char) -> Csv§

    Source

    Fields separated by c (one ASCII character).

  • pub fn has_header(self, yes: bool) -> Csv§

    Source

    Whether the first line names the columns.

  • pub fn null(self, token: str) -> Csv§

    Source

    Also read token as null (and write nulls as it).

  • pub fn null_tokens(self, tokens: List<str>) -> Csv§

    Source

    Exactly these tokens are nulls; the first is written for a null.

  • pub fn types(self, ts: List<(str, DType)>) -> Csv§

    Source

    Fix the types of some columns by name.

  • pub fn batch_rows(self, n: usize) -> Csv§

    Source

    Rows per batch of CsvReader.

  • pub fn trimmed(self, yes: bool) -> Csv§

    Source

    Strip blanks around unquoted fields.

  • pub fn crlf_lines(self, yes: bool) -> Csv§

    Source

    \r\n line ends when writing.

  • pub fn dialect(self) -> csvio::Dialect§

    Source

    The csv dialect these options read and write with.

    nabi
    let d = table::Csv::new().delimiter(';').dialect();
    assert(d.delim == 0x3Bu8 && d.header);
    
  • pub fn read_str(self, s: str) throws (TableError) -> Table§

    Source

    A table from CSV text, every row used to pick the types.

  • pub fn read_file(self, path: str) throws (TableError) -> Table§

    Source

    A table from the CSV file at path.

  • pub fn reader<R: Read>(self, r: R) -> CsvReader§

    Source

    A streaming reader over r.

  • pub fn write_str(self, t: Table) -> str§

    Source

    The table as CSV text.

    nabi
    let t = table::table!{ "a": [1, none], "b": ["x,y", "q\""] };
    assert(table::Csv::new().write_str(t) == "a,b\n1,\"x,y\"\n,\"q\"\"\"\n");
    
  • pub fn write_file(self, t: Table, path: str) throws (TableError)§

    Source

    Write the table to the file at path.

CsvReaderSource

pub struct CsvReader

Reads CSV from a stream in batches. Types come from the first infer_rows rows (or Csv::types); a later cell that does not fit is an error that says which column to fix.

nabi
let t = table::table!{ "i": [1, 2, 3, 4, 5] };
let text = table::Csv::new().write_str(t);
let r = table::Csv::new().batch_rows(2).reader(core::io::Cursor::new(text.as_bytes().to_list()));
let total = 0;
loop {
    match try r.next() catch _ { none } {
        b: table::Table => { total += b.nrows(); },
        none => break,
    }
}
assert(total == 5);
  • pub fn names(self) -> List<str>§

    Source

    The column names (after the first next).

  • pub fn next(ref self) throws (TableError) -> Table | none§

    Source

    The next batch of up to batch rows; none after the last.

  • pub fn read_all(ref self) throws (TableError) -> Table§

    Source

    Every remaining batch as one table.

Functions

read_csvSource

pub fn read_csv(path: str) throws (TableError) -> Table

Read the CSV file at path with the default options.

nabi
let t = try "data.csv" |> table::read_csv;

read_csv_strSource

pub fn read_csv_str(s: str) throws (TableError) -> Table

A table from CSV text with the default options.

nabi
let t = try table::read_csv_str("a,b\n1,x\n2,y\n") catch _ { table::Table::new() };
assert(t.shape() == (2, 2));

write_csvSource

pub fn write_csv(t: Table, path: str) throws (TableError)

Write the table as CSV to path with the default options.

nabi
let t = table::table!{ "a": [1, 2] };
try t |> table::write_csv("out.csv");