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
decodeTyped rows.dialectDialect: how a CSV file is spelled.errorCsvError: what went wrong, where (line and column, 1-based), and what to write instead.readReader: records from a stream, astror bytes.recordRecord: one row's fields as views of the text it was read from, andHeaders, the header's names with a lookup by name.writeWriter: records out, each field quoted only as the dialect'sQuoteStylesays, 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}ColumnsWhere each field of a row type is in the records: a position per field,usize::MAXfor a column the header lacks.CursorReads one row's fields in order, for#[derive(Csv)].FromFieldA type one field converts to.FromRecordA row type:#[derive(Csv)]writes it.RowsReader::decode: the rows asTs.ToFieldA type that writes as one field.ToRecordA row type the writer takes:#[derive(Csv)]writes it.
dialect::{Dialect, QuoteStyle, Trim}Source
pub use dialect::{Dialect, QuoteStyle, Trim}DialectA CSV dialect: delimiter, quoting, comments, trimming and the writer's line end.QuoteStyleWhen the writer quotes a field.TrimWhat to strip of blanks (spaces and tabs) around fields.
error::{CsvError, ErrorKind}Source
pub use error::{CsvError, ErrorKind}read::ReaderSource
pub use read::ReaderReaderReads records.
record::{Fields, Headers, Record}Source
pub use record::{Fields, Headers, Record}write::WriterSource
pub use write::WriterWriterWrites records.
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");