Skip to main content

Module csv

Module csv 

Source
Expand description

CSV and TSV: a port of Python’s csv sniffer and reader, and a CsvReader that turns a file into Rows.

sniff, has_header and read_rows are the reader rich --csv has always used (moved here from rs-rich-cli in 0.0.16 so every caller shares it): csv.Sniffer.sniff, csv.Sniffer.has_header and _csv.reader, ported closely enough that rich --csv keeps rich-cli 1.8.1’s output byte for byte. They take text that has already been through Python’s universal newlines (no \r); CsvReader does that first.

CsvReader is the adapter: it sniffs the dialect and the header the way --csv does (or takes them as given), drops blank lines, and yields every cell as Value::Str exactly as written. Nothing is typed until infer is asked to.

use rich_data::csv::CsvReader;
use rich_data::Value;

let rows = CsvReader::new().read("name;age\nAda;36\nAlan;41\n").unwrap();
assert_eq!(rows.columns(), ["name", "age"]);
assert_eq!(rows.rows()[1], [Value::from("Alan"), Value::from("41")]);

let tsv = CsvReader::tsv().header(false).read("a\tb\n").unwrap();
assert_eq!(tsv.columns(), ["1", "2"]);

Structs§

CsvReader
Reads CSV or TSV text into Rows. See the module docs.
CsvSource
A streaming CSV or TSV RowSource, from CsvReader::source. Every cell is Value::Str as written.
Dialect
A CSV dialect: everything csv.Sniffer decides and csv.reader consumes.

Enums§

Header
Whether the first row is a header.

Constants§

DELIMITERS
The delimiters CsvReader (and rich --csv) lets the sniffer choose from: rich-cli’s ",\t|;".
MAX_RECORD
The longest record a CsvSource holds, in bytes (64 MiB): a line, or a quoted field still open, past it is refused rather than read into memory.
SAMPLE_CHARS
How many characters of the input the sniffer reads: rich-cli’s csv_data[:1024].

Functions§

has_header
Port of csv.Sniffer.has_header: if a column is one consistent type in every row except the first, the first row is a label. Each column casts one vote.
read_rows
Parse content into rows of fields with dialect. Port of _csv.reader’s state machine, minus the escape character (no sniffed dialect sets one) and strict (always off, as QUOTE_MINIMAL leaves it).
sniff
Port of csv.Sniffer.sniff, restricted to delimiters when given (rich-cli passes ",\t|;"). None is CPython’s csv.Error: no delimiter found.