packages

csv

On this page

CSV (RFC 4180) and its common dialects: a streaming Reader over any Read, a str or bytes; Records whose fields are views of the input; typed rows with #[derive(Csv)], matched to the header by name; and a Writer that quotes only what needs it.

The reader finds delimiters, quotes and line ends 16 bytes at a time with core::simd, so the bytes of a field are not looked at one by one. A stream is read in chunks (256 KiB by default): memory stays flat however long the input.

Modules

  • decode Typed rows.
  • dialect Dialect: how a CSV file is spelled.
  • error CsvError: what went wrong, where (line and column, 1-based), and what to write instead.
  • read Reader: records from a stream, a str or bytes.
  • record Record: one row's fields as views of the text it was read from, and Headers, the header's names with a lookup by name.
  • write Writer: records out, each field quoted only as the dialect's QuoteStyle says, to a stream or to memory.

Re-exports

decode::{Columns, Cursor, FromField, FromRecord, Rows, ToField, ToRecord}Source

pub use decode::{Columns, Cursor, FromField, FromRecord, Rows, ToField, ToRecord}
  • Columns Where each field of a row type is in the records: a position per field, usize::MAX for a column the header lacks.
  • Cursor Reads one row's fields in order, for #[derive(Csv)].
  • FromField A type one field converts to.
  • FromRecord A row type: #[derive(Csv)] writes it.
  • Rows Reader::decode: the rows as Ts.
  • ToField A type that writes as one field.
  • ToRecord A row type the writer takes: #[derive(Csv)] writes it.

dialect::{Dialect, QuoteStyle, Trim}Source

pub use dialect::{Dialect, QuoteStyle, Trim}
  • Dialect A CSV dialect: delimiter, quoting, comments, trimming and the writer's line end.
  • QuoteStyle When the writer quotes a field.
  • Trim What to strip of blanks (spaces and tabs) around fields.

error::{CsvError, ErrorKind}Source

pub use error::{CsvError, ErrorKind}

read::ReaderSource

pub use read::Reader

record::{Fields, Headers, Record}Source

pub use record::{Fields, Headers, Record}
  • Fields The fields of a Record, in order.
  • Headers The header's names, with a lookup by name.
  • Record One record.

write::WriterSource

pub use write::Writer

Functions

readerSource

pub fn reader<R: Read>(r: R) -> Reader

A reader over the stream r with Dialect::new(): a header line, , between fields.

nabi
let f = try! std::fs::open("prices.csv");
let r = csv::reader(f);
try! for rec in r { println(rec[0]); }

from_strSource

pub fn from_str(s: str) -> Reader

A reader over the text s with Dialect::new().

nabi
let r = csv::from_str("a,b\n1,2\n");
assert((try! r.next() ?? csv::Record::new()).to_list() == ["1", "2"]);

from_bytesSource

pub fn from_bytes(b: List<u8>) -> Reader

A reader over UTF-8 bytes with Dialect::new().

nabi
let r = csv::from_bytes("a\n1\n".as_bytes().to_list());
assert(try! r.count() == 1);

writerSource

pub fn writer<W: Write>(w: W) -> Writer

A writer to the stream w with Dialect::new().

nabi
let out: List<u8> = List::new();
let w = csv::writer(core::io::Cursor::new(out));
try! w.write_record(["a", "b"]);
try! w.flush();
assert(out.len() == 4);

to_strSource

pub fn to_str<T: ToRecord>(rows: List<T>) throws (CsvError) -> str

rows as CSV text with a header, Dialect::new().

nabi
#[derive(csv::Csv)]
struct P { name: str; n: i32; }

let s = try! csv::to_str([P { name: "a,b", n: 1 }]);
assert(s == "name,n\n\"a,b\",1\n");