packages

csv::read

On this page

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 Reader

Reads 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.

nabi
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);
  • pub fn headers(self) throws (CsvError) -> Headers§

    Source

    The header (read now if no record was read yet); empty when the dialect has none.

    nabi
    let r = csv::from_str("a,b\n1,2\n");
    assert((try! r.headers()).names() == ["a", "b"]);
    
  • pub fn read(self, ref rec: Record) throws (CsvError) -> bool§

    Source

    The next record into rec; false at the end. Unlike next, it makes no new Record per row.

    nabi
    let 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);
    
  • pub fn line(self) -> usize§

    Source

    The line the reader is at, 1-based: where the next record starts.

    nabi
    let r = csv::from_str("a\n1\n2\n");
    _ = try! r.next();
    assert(r.line() == 3);
    
  • pub fn position(self) -> u64§

    Source

    The byte offset the reader is at: where the next record starts.

    nabi
    let r = csv::from_str("a\n1\n");
    _ = try! r.next();
    assert(r.position() == 4);
    
  • pub fn read_columns(self, cols: List<List<str>>, quoted: List<List<usize>>, max: usize) throws (CsvError) -> usize§

    Source

    Up to max records appended column by column: field k of each to cols[k], and its place in cols[k] to quoted[k] when it was quoted. It stops early before a record with another field count than cols has (read gets that one) and at the end; the count. For column stores: no Record per row.

    nabi
    let 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]);
    
  • pub fn count(self) throws (CsvError) -> usize§

    Source

    The number of records left, read to the end.

    nabi
    assert(try! csv::from_str("a\n1\n2\n").count() == 2);
    
  • pub fn read_all(self) throws (CsvError) -> List<Record>§

    Source

    Every record left.

    nabi
    let all = try! csv::from_str("a\n1\n2\n").read_all();
    assert(all.len() == 2 && all[1][0] == "2");
    
  • pub fn decode<T: FromRecord>(self) -> Rows<T>§

    Source

    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