commit | b443023299102e44c0080fe7dce329ab33dcbcae | [log] [tgz] |
---|---|---|
author | Jeff Vander Stoep <[email protected]> | Fri Feb 17 13:41:47 2023 +0000 |
committer | Automerger Merge Worker <[email protected]> | Fri Feb 17 13:41:47 2023 +0000 |
tree | 34b7bf7672f8daaef8d71ab399d959b47813bbc9 | |
parent | 0e105dfc9f68bbfad6e41e359e1f2a8a9cf1f0d0 [diff] | |
parent | a29ba88be8ee9ae546617a2139dd76d221a4f72e [diff] |
Upgrade csv to 1.2.0 am: 98e901282b am: e6a034caf1 am: a29ba88be8 Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/csv/+/2438288 Change-Id: I8d891aacc1bfafda8cf0405f8320da5a332d3471 Signed-off-by: Automerger Merge Worker <[email protected]>
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.2"
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, io, 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, io, process}; #[derive(Debug, serde::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