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 CsvHow to read or write CSV. Start from Csv::new() and change what
differs.
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);
The field separator,
,by default.The first line names the columns (else
column_1, ...).Cells read as null; written for a null.
Column types that skip inference.
Rows looked at to pick types when streaming (
CsvReader).Rows per
CsvReader::nexttable.Write
\r\nline ends (RFC 4180);\nby default.Strip whitespace around unquoted fields.
Comma-separated, with a header;
"",NA,N/A,null,NULLread as null.Fields separated by
c(one ASCII character).Whether the first line names the columns.
Also read
tokenas null (and write nulls as it).Exactly these tokens are nulls; the first is written for a null.
Fix the types of some columns by name.
Rows per batch of
CsvReader.Strip blanks around unquoted fields.
\r\nline ends when writing.The
csvdialect these options read and write with.nabilet d = table::Csv::new().delimiter(';').dialect(); assert(d.delim == 0x3Bu8 && d.header);
Sourcepub fn read_str(§self, s: str) throws (TableError) -> Table A table from CSV text, every row used to pick the types.
Sourcepub fn read_file(§self, path: str) throws (TableError) -> Table A table from the CSV file at
path.A streaming reader over
r.The table as CSV text.
nabilet 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");
Sourcepub fn write_file(§self, t: Table, path: str) throws (TableError) Write the table to the file at
path.
CsvReaderSource
pub struct CsvReaderReads 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.
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);
The column names (after the first
next).
Sourcepub fn next(§ref self) throws (TableError) -> Table | none The next batch of up to
batchrows;noneafter the last.
Sourcepub fn read_all(§ref self) throws (TableError) -> Table 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.
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.
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.
let t = table::table!{ "a": [1, 2] };
try t |> table::write_csv("out.csv");