commit | 226e0c897f9ab99db0fd05c4dca6ac0421beaf0c | [log] [tgz] |
---|---|---|
author | Jakub Kotur <qtr@google.com> | Tue Mar 16 20:52:59 2021 +0000 |
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | Tue Mar 16 20:52:59 2021 +0000 |
tree | ed071da671759af887bfdfa66765f87f0f7c464e | |
parent | dec626d8799261f647365528ad9d213de4371e41 [diff] | |
parent | d4fd3a6082e5747ff4862e268d2a8b71be15ff69 [diff] |
Initial import of csv-1.1.5. am: c72d7208a5 am: 4db6b4b613 am: 1582919329 am: d4fd3a6082 Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/csv/+/1621391 Change-Id: I035e85ec93561c4fe98e100499fdbf3d1245a3d0
A fast and flexible CSV reader and writer for Rust, with support for Serde.
Dual-licensed under MIT or the UNLICENSE.
If you're new to Rust, the tutorial is a good place to start.
Add this to your Cargo.toml
:
[dependencies] csv = "1.1"
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
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