pub struct CsvReader { /* private fields */ }Expand description
Reads CSV or TSV text into Rows. See the module docs.
Implementations§
Source§impl CsvReader
impl CsvReader
Sourcepub fn new() -> Self
pub fn new() -> Self
Sniff the dialect from DELIMITERS and the header, failing when the
sniffer finds no delimiter (set a fallback to
read such input anyway).
Sourcepub fn delimiter(self, delimiter: char) -> Self
pub fn delimiter(self, delimiter: char) -> Self
Use this delimiter, with excel’s quoting, instead of sniffing.
Sourcepub fn fallback(self, delimiter: char) -> Self
pub fn fallback(self, delimiter: char) -> Self
The delimiter to fall back to when the sniffer finds none, as
rich --csv falls back to , for a .csv file and a tab for a
.tsv one.
Sourcepub fn header(self, header: bool) -> Self
pub fn header(self, header: bool) -> Self
Whether the first row is a header (true/false), instead of
sniffing it.
Sourcepub fn header_mode(self, header: Header) -> Self
pub fn header_mode(self, header: Header) -> Self
Set the header decision, Header::Sniff included.
Source§impl CsvReader
impl CsvReader
Sourcepub fn source<R: BufRead>(&self, reader: R) -> Result<CsvSource<R>, DataError>
pub fn source<R: BufRead>(&self, reader: R) -> Result<CsvSource<R>, DataError>
Stream rows from reader instead of reading the text whole, so an
input larger than memory can be read (a profile
samples it).
The dialect and header are decided from the first SAMPLE_CHARS
characters, as read decides them, and records are
parsed one at a time: a quoted field may span lines, up to
MAX_RECORD bytes. The columns are decided from the rows the
sniffer read, as read decides them from every row (a
row longer than the header adds columns named by position); a later
row is padded with nulls or cut to fit, and
longer_rows counts the cut ones. Bytes
that are not UTF-8 are replaced.
use rich_data::csv::CsvReader;
use rich_data::{RowSource, Value};
let text = "name,note\nweb,\"two\nlines\"\napi,ok\n";
let mut source = CsvReader::new().source(text.as_bytes()).unwrap();
assert_eq!(source.columns(), ["name", "note"]);
let first = source.next_row().unwrap().unwrap();
assert_eq!(first[1], Value::from("two\nlines"));
source.next_row().unwrap().unwrap();
assert!(source.next_row().is_none());