csv::read
Reader: records from a stream, a str or bytes. A stream is read
in chunks into one buffer; the complete lines of a chunk become one
str, and every field is a view of it. Records are split with the
SIMD marks of scan.
Structs
ReaderSource
pub struct ReaderReads records. next gives the data records (the header, when the
dialect has one, is read first and kept in headers); read
fills a record you keep, which saves an allocation per row. The
reader scans up to 256 records ahead per call, so per-record work
stays small;
decode turns rows into structs. Copies of a reader share its
place in the input.
let r = csv::from_str("city,pop\nOslo,709000\nBergen,291000\n");
let total = 0;
try! for rec in r {
total += try! rec[1].parse::<i64>();
}
assert(total == 1000000);
The header (read now if no record was read yet); empty when the dialect has none.
nabilet r = csv::from_str("a,b\n1,2\n"); assert((try! r.headers()).names() == ["a", "b"]);The next record into
rec;falseat the end. Unlikenext, it makes no newRecordper row.nabilet r = csv::from_str("n\n1\n2\n3\n"); let rec = csv::Record::new(); let sum = 0; while try! r.read(ref rec) { sum += try! rec[0].parse::<i64>(); } assert(sum == 6);The line the reader is at, 1-based: where the next record starts.
nabilet r = csv::from_str("a\n1\n2\n"); _ = try! r.next(); assert(r.line() == 3);The byte offset the reader is at: where the next record starts.
nabilet r = csv::from_str("a\n1\n"); _ = try! r.next(); assert(r.position() == 4);
Sourcepub fn read_columns(§self, cols: List<List<str>>, quoted: List<List<usize>>, max: usize) throws (CsvError) -> usize Up to
maxrecords appended column by column: fieldkof each tocols[k], and its place incols[k]toquoted[k]when it was quoted. It stops early before a record with another field count thancolshas (readgets that one) and at the end; the count. For column stores: noRecordper row.nabilet r = csv::from_str("a,b\n1,\"x\"\n2,y\n"); let cols: List<List<str>> = [List::new(), List::new()]; let quoted: List<List<usize>> = [List::new(), List::new()]; assert(try! r.read_columns(cols, quoted, 100) == 2); assert(cols[0] == ["1", "2"] && cols[1] == ["x", "y"] && quoted[1] == [0usize]);The number of records left, read to the end.
nabiassert(try! csv::from_str("a\n1\n2\n").count() == 2);Every record left.
nabilet all = try! csv::from_str("a\n1\n2\n").read_all(); assert(all.len() == 2 && all[1][0] == "2");
Sourcepub fn decode<T: FromRecord>(§self) -> Rows<T> The rows as
Ts, fields matched to the header by name (by position without a header).nabi#[derive(csv::Csv)] struct City { name: str; pop: u32; } let r = csv::from_str("pop,name\n709000,Oslo\n"); let c = try! r.decode::<City>().next() ?? City { name: "", pop: 0 }; assert(c.name == "Oslo" && c.pop == 709000);
Trait implementations 1
impl TryIterator for Reader