packages

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 Dialect

A CSV dialect: delimiter, quoting, comments, trimming and the writer's line end.

nabi
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");
  • pub delim: u8§

    Source

    The field separator, , by default.

  • pub quote: u8 | none§

    Source

    The quote, " by default; none reads quotes as plain text.

  • pub escape: u8 | none§

    Source

    An escape inside quotes (often \): it makes the next byte plain text. none by default.

  • pub double_quote: bool§

    Source

    A doubled quote inside quotes is one quote (RFC 4180).

  • pub comment: u8 | none§

    Source

    A line starting with this byte is skipped. none by default.

  • pub trim: Trim§

    Source

    Blanks stripped around fields.

  • pub flexible: bool§

    Source

    Records may have different field counts.

  • pub header: bool§

    Source

    The first record is the header.

  • pub skip_blank: bool§

    Source

    Empty lines are skipped (else each is a record of one empty field).

  • pub line_end: str§

    Source

    What the writer ends each record with, \n by default.

  • pub quote_style: QuoteStyle§

    Source

    When the writer quotes.

  • pub bom: bool§

    Source

    The writer starts with a UTF-8 byte order mark (for Excel).

  • pub chunk: usize§

    Source

    Bytes read from a stream at a time.

  • pub fn new() -> Dialect§

    Source

    RFC 4180: , between fields, " quotes with "" for a quote, a header line, blank lines skipped; the writer ends lines with \n.

    nabi
    let d = csv::Dialect::new();
    assert(d.delim == 0x2Cu8 && d.header);
    
  • pub fn tsv() -> Dialect§

    Source

    Tab-separated values: Dialect::new() with a tab between fields.

    nabi
    let rec = try! csv::Dialect::tsv().from_str("a\tb\n1\t2\n").next() ?? csv::Record::new();
    assert(rec.to_list() == ["1", "2"]);
    
  • pub fn delimiter(self, c: char) -> Dialect§

    Source

    Fields separated by c, one ASCII character other than a quote or a line break.

    nabi
    assert(csv::Dialect::new().delimiter(';').delim == 0x3Bu8);
    
  • pub fn quote(self, c: char) -> Dialect§

    Source

    The quote character (ASCII).

    nabi
    let rec = try! csv::Dialect::new().quote('\'').has_header(false).from_str("'a,b',c\n").next() ?? csv::Record::new();
    assert(rec[0] == "a,b");
    
  • pub fn no_quotes(self) -> Dialect§

    Source

    Quotes are plain text: no field is quoted.

    nabi
    let 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"]);
    
  • pub fn escape(self, c: char) -> Dialect§

    Source

    An escape inside quotes: \" is a quote and \\ a backslash when c is \. The writer escapes quotes with it unless double_quotes(true) is also set.

    nabi
    let 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\"");
    
  • pub fn double_quotes(self, yes: bool) -> Dialect§

    Source

    Whether "" inside quotes is one quote (true by default).

    nabi
    assert(!csv::Dialect::new().double_quotes(false).double_quote);
    
  • pub fn comment(self, c: char) -> Dialect§

    Source

    Lines starting with c are comments and skipped.

    nabi
    let 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"]);
    
  • pub fn trim(self, t: Trim) -> Dialect§

    Source

    Strip blanks around fields, outside quotes.

    nabi
    let 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 "]);
    
  • pub fn flexible(self, yes: bool) -> Dialect§

    Source

    Allow records with different field counts. Off by default: a record that differs from the first is an error.

    nabi
    let 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);
    
  • pub fn has_header(self, yes: bool) -> Dialect§

    Source

    Whether the first record is the header (true by default). The header is not returned by Reader::next; Reader::headers has it.

    nabi
    let r = csv::Dialect::new().has_header(false).from_str("a,b\n");
    assert((try! r.next() ?? csv::Record::new())[0] == "a");
    
  • pub fn skip_blank_lines(self, yes: bool) -> Dialect§

    Source

    Whether empty lines are skipped (true by default). When kept, each is a record with one empty field.

    nabi
    let 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() == [""]);
    
  • pub fn crlf(self, yes: bool) -> Dialect§

    Source

    End written records with \r\n (RFC 4180) instead of \n.

    nabi
    let w = csv::Dialect::new().crlf(true).to_buffer();
    try! w.write_record(["a", "b"]);
    assert(w.as_str() == "a,b\r\n");
    
  • pub fn quote_style(self, s: QuoteStyle) -> Dialect§

    Source

    When the writer quotes fields.

    nabi
    let w = csv::Dialect::new().quote_style(csv::QuoteStyle::Always).to_buffer();
    try! w.write_record(["a", "1"]);
    assert(w.as_str() == "\"a\",\"1\"\n");
    
  • pub fn write_bom(self, yes: bool) -> Dialect§

    Source

    Start the written output with a UTF-8 byte order mark, which Excel needs to read UTF-8. Readers always skip one.

    nabi
    let w = csv::Dialect::new().write_bom(true).to_buffer();
    try! w.write_record(["é"]);
    assert(w.as_str() == "\u{FEFF}é\n");
    
  • pub fn buffer_size(self, n: usize) -> Dialect§

    Source

    Read streams n bytes at a time (256 KiB by default). A record longer than that grows the buffer.

    nabi
    assert(csv::Dialect::new().buffer_size(4096).chunk == 4096);
    
  • pub fn reader<R: Read>(self, r: R) -> Reader§

    Source

    A reader over the stream r, read in chunks.

    nabi
    let 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");
    
  • pub fn from_str(self, s: str) -> Reader§

    Source

    A reader over the text s. Fields are views of s, not copies.

    nabi
    let r = csv::Dialect::new().from_str("x\n1\n2\n");
    assert(try! r.count() == 2);
    
  • pub fn from_bytes(self, b: List<u8>) -> Reader§

    Source

    A reader over UTF-8 bytes; not UTF-8 is an error on the first read.

    nabi
    let r = csv::Dialect::new().from_bytes("a\nä\n".as_bytes().to_list());
    assert((try! r.next() ?? csv::Record::new())[0] == "ä");
    
  • pub fn writer<W: Write>(self, w: W) -> Writer§

    Source

    A writer to the stream w; flush (or close) writes what is buffered.

    nabi
    let out: List<u8> = List::new();
    let w = csv::Dialect::new().writer(core::io::Cursor::new(out));
    try! w.write_record(["a"]);
    try! w.flush();
    
  • pub fn to_buffer(self) -> Writer§

    Source

    A writer that keeps the output in memory: as_str gives it.

    nabi
    let 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 Trim

What to strip of blanks (spaces and tabs) around fields. Blanks inside quotes are kept.

nabi
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");

QuoteStyleSource

pub enum QuoteStyle

When the writer quotes a field.

nabi
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");
  • Necessary§

    Source

    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.

  • Always§

    Source

    Every field.

  • NonNumeric§

    Source

    Every field that does not read as a number.

  • No field; a field that needs quotes is an error.