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, fromCsvReader::source. Every cell isValue::Stras written. - Dialect
- A CSV dialect: everything
csv.Snifferdecides andcsv.readerconsumes.
Enums§
- Header
- Whether the first row is a header.
Constants§
- DELIMITERS
- The delimiters
CsvReader(andrich --csv) lets the sniffer choose from: rich-cli’s",\t|;". - MAX_
RECORD - The longest record a
CsvSourceholds, 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
contentinto rows of fields withdialect. Port of_csv.reader’s state machine, minus the escape character (no sniffed dialect sets one) andstrict(always off, asQUOTE_MINIMALleaves it). - sniff
- Port of
csv.Sniffer.sniff, restricted todelimiterswhen given (rich-cli passes",\t|;").Noneis CPython’scsv.Error: no delimiter found.