71 releases (10 stable)

✓ Uses Rust 2018 edition

1.1.1 Jul 1, 2019
1.0.7 Apr 13, 2019
1.0.5 Dec 16, 2018
1.0.2 Sep 25, 2018
0.7.0 Nov 28, 2014

#1 in Parser implementations

Download history 27227/week @ 2019-07-25 26838/week @ 2019-08-01 29443/week @ 2019-08-08 29368/week @ 2019-08-15 29907/week @ 2019-08-22 29935/week @ 2019-08-29 31403/week @ 2019-09-05 31805/week @ 2019-09-12 33710/week @ 2019-09-19 36292/week @ 2019-09-26 40768/week @ 2019-10-03 38922/week @ 2019-10-10 39437/week @ 2019-10-17 40474/week @ 2019-10-24 36891/week @ 2019-10-31

99,282 downloads per month
Used in 941 crates (219 directly)

Unlicense/MIT

1MB
7K SLoC

csv

A fast and flexible CSV reader and writer for Rust, with support for Serde.

Linux build status Windows build status

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/csv

If you're new to Rust, the tutorial is a good place to start.

Usage

Add this to your Cargo.toml:

[dependencies]
csv = "1.1"

Example

This example shows how to read CSV data from stdin and print each record to stdout.

There are more examples in the cookbook.

use std::error::Error;
use std::io;
use std::process;

fn example() -> Result<(), Box<dyn Error>> {
    // Build the CSV reader and iterate over each record.
    let mut rdr = csv::Reader::from_reader(io::stdin());
    for result in rdr.records() {
        // The iterator yields Result<StringRecord, Error>, so we check the
        // error here.
        let record = result?;
        println!("{:?}", record);
    }
    Ok(())
}

fn main() {
    if let Err(err) = example() {
        println!("error running example: {}", err);
        process::exit(1);
    }
}

The above example can be run like so:

$ git clone git://github.com/BurntSushi/rust-csv
$ cd rust-csv
$ cargo run --example cookbook-read-basic < examples/data/smallpop.csv

Example with Serde

This example shows how to read CSV data from stdin into your own custom struct. By default, the member names of the struct are matched with the values in the header record of your CSV data.

use std::error::Error;
use std::io;
use std::process;

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Record {
    city: String,
    region: String,
    country: String,
    population: Option<u64>,
}

fn example() -> Result<(), Box<dyn Error>> {
    let mut rdr = csv::Reader::from_reader(io::stdin());
    for result in rdr.deserialize() {
        // Notice that we need to provide a type hint for automatic
        // deserialization.
        let record: Record = result?;
        println!("{:?}", record);
    }
    Ok(())
}

fn main() {
    if let Err(err) = example() {
        println!("error running example: {}", err);
        process::exit(1);
    }
}

The above example can be run like so:

$ git clone git://github.com/BurntSushi/rust-csv
$ cd rust-csv
$ cargo run --example cookbook-read-serde < examples/data/smallpop.csv

Dependencies