csv::dialect
On this page
Dialect: how a CSV file is spelled. Dialect::new() is RFC 4180
with a header line; each method returns a copy with one thing
changed, and reader, from_str, from_bytes and writer start
reading or writing with it.
Structs
DialectSource
pub struct DialectA CSV dialect: delimiter, quoting, comments, trimming and the writer's line end.
let d = csv::Dialect::new().delimiter(';').comment('#').trim(csv::Trim::All);
let r = d.from_str("# prices\nname ; eur\nbread ; 2,40\n");
let rec = try! r.next() ?? csv::Record::new();
assert(rec[1] == "2,40");
The field separator,
,by default.The quote,
"by default;nonereads quotes as plain text.An escape inside quotes (often
\): it makes the next byte plain text.noneby default.A doubled quote inside quotes is one quote (RFC 4180).
A line starting with this byte is skipped.
noneby default.Blanks stripped around fields.
Records may have different field counts.
The first record is the header.
Empty lines are skipped (else each is a record of one empty field).
What the writer ends each record with,
\nby default.
Sourcepub quote_style: QuoteStyle§When the writer quotes.
The writer starts with a UTF-8 byte order mark (for Excel).
Bytes read from a stream at a time.
RFC 4180:
,between fields,"quotes with""for a quote, a header line, blank lines skipped; the writer ends lines with\n.nabilet d = csv::Dialect::new(); assert(d.delim == 0x2Cu8 && d.header);Tab-separated values:
Dialect::new()with a tab between fields.nabilet rec = try! csv::Dialect::tsv().from_str("a\tb\n1\t2\n").next() ?? csv::Record::new(); assert(rec.to_list() == ["1", "2"]);Fields separated by
c, one ASCII character other than a quote or a line break.nabiassert(csv::Dialect::new().delimiter(';').delim == 0x3Bu8);The quote character (ASCII).
nabilet rec = try! csv::Dialect::new().quote('\'').has_header(false).from_str("'a,b',c\n").next() ?? csv::Record::new(); assert(rec[0] == "a,b");Quotes are plain text: no field is quoted.
nabilet rec = try! csv::Dialect::new().no_quotes().has_header(false).from_str("\"a,b\n").next() ?? csv::Record::new(); assert(rec.to_list() == ["\"a", "b"]);An escape inside quotes:
\"is a quote and\\a backslash whencis\. The writer escapes quotes with it unlessdouble_quotes(true)is also set.nabilet d = csv::Dialect::new().escape('\\').double_quotes(false).has_header(false); let rec = try! d.from_str("\"say \\\"hi\\\"\",x\n").next() ?? csv::Record::new(); assert(rec[0] == "say \"hi\"");Whether
""inside quotes is one quote (trueby default).nabiassert(!csv::Dialect::new().double_quotes(false).double_quote);Lines starting with
care comments and skipped.nabilet r = csv::Dialect::new().comment('#').has_header(false).from_str("# note\n1,2\n"); assert((try! r.next() ?? csv::Record::new()).to_list() == ["1", "2"]);Strip blanks around fields, outside quotes.
nabilet r = csv::Dialect::new().trim(csv::Trim::All).has_header(false).from_str(" a , \"b \" \n"); assert((try! r.next() ?? csv::Record::new()).to_list() == ["a", "b "]);Allow records with different field counts. Off by default: a record that differs from the first is an error.
nabilet r = csv::Dialect::new().flexible(true).has_header(false).from_str("1,2\n3\n"); _ = try! r.next(); assert((try! r.next() ?? csv::Record::new()).len() == 1);Whether the first record is the header (
trueby default). The header is not returned byReader::next;Reader::headershas it.nabilet r = csv::Dialect::new().has_header(false).from_str("a,b\n"); assert((try! r.next() ?? csv::Record::new())[0] == "a");Whether empty lines are skipped (
trueby default). When kept, each is a record with one empty field.nabilet r = csv::Dialect::new().skip_blank_lines(false).has_header(false).from_str("a\n\nb\n"); _ = try! r.next(); assert((try! r.next() ?? csv::Record::new()).to_list() == [""]);End written records with
\r\n(RFC 4180) instead of\n.nabilet w = csv::Dialect::new().crlf(true).to_buffer(); try! w.write_record(["a", "b"]); assert(w.as_str() == "a,b\r\n");
Sourcepub fn quote_style(§self, s: QuoteStyle) -> Dialect When the writer quotes fields.
nabilet w = csv::Dialect::new().quote_style(csv::QuoteStyle::Always).to_buffer(); try! w.write_record(["a", "1"]); assert(w.as_str() == "\"a\",\"1\"\n");Start the written output with a UTF-8 byte order mark, which Excel needs to read UTF-8. Readers always skip one.
nabilet w = csv::Dialect::new().write_bom(true).to_buffer(); try! w.write_record(["é"]); assert(w.as_str() == "\u{FEFF}é\n");Read streams
nbytes at a time (256 KiB by default). A record longer than that grows the buffer.nabiassert(csv::Dialect::new().buffer_size(4096).chunk == 4096);A reader over the stream
r, read in chunks.nabilet bytes = "a,b\n1,2\n".as_bytes().to_list(); let r = csv::Dialect::new().reader(core::io::Cursor::new(bytes)); assert((try! r.next() ?? csv::Record::new())[1] == "2");A reader over the text
s. Fields are views ofs, not copies.nabilet r = csv::Dialect::new().from_str("x\n1\n2\n"); assert(try! r.count() == 2);A reader over UTF-8 bytes; not UTF-8 is an error on the first read.
nabilet r = csv::Dialect::new().from_bytes("a\nä\n".as_bytes().to_list()); assert((try! r.next() ?? csv::Record::new())[0] == "ä");A writer to the stream
w;flush(orclose) writes what is buffered.nabilet out: List<u8> = List::new(); let w = csv::Dialect::new().writer(core::io::Cursor::new(out)); try! w.write_record(["a"]); try! w.flush();A writer that keeps the output in memory:
as_strgives it.nabilet w = csv::Dialect::new().to_buffer(); try! w.write_record(["a,b", "c"]); assert(w.as_str() == "\"a,b\",c\n");
Trait implementations 1
impl Default for Dialect
Enums
TrimSource
pub enum TrimWhat to strip of blanks (spaces and tabs) around fields. Blanks inside quotes are kept.
let r = csv::Dialect::new().trim(csv::Trim::Fields).from_str(" a \n x \n");
assert((try! r.headers()).names() == [" a "]);
assert((try! r.next() ?? csv::Record::new())[0] == "x");
Keep every byte.
Trim the header's names only.
Trim data fields only.
Trim both.
QuoteStyleSource
pub enum QuoteStyleWhen the writer quotes a field.
let w = csv::Dialect::new().quote_style(csv::QuoteStyle::NonNumeric).to_buffer();
try! w.write_record(["a", "1.5"]);
assert(w.as_str() == "\"a\",1.5\n");
Only fields that need it: they hold the delimiter, a quote, a line break, start or end with a blank, or are the empty only field of a record.
Every field.
Every field that does not read as a number.
No field; a field that needs quotes is an error.