commit | c8d9f9dbe47b47b6ca5c96881776d77fb29f8866 | [log] [tgz] |
---|---|---|
author | Xin Li <[email protected]> | Mon Apr 29 22:54:47 2024 +0000 |
committer | Automerger Merge Worker <[email protected]> | Mon Apr 29 22:54:47 2024 +0000 |
tree | 309bdc33842825215ebab815894e14d3e21dc6a1 | |
parent | ae0ec7e1ff55b03d049ae952392454ac6b45524b [diff] | |
parent | 363c786beddca56f6db3c96474e27d1aeba991a3 [diff] |
[automerger skipped] Empty merge of Android 24Q2 Release (ab/11526283) to aosp-main-future am: 363c786bed -s ours am skip reason: Merged-In I88a16b778e3112f1feee4525fecdd40b54d2706d with SHA-1 337ee94e41 is already in history Original change: https://googleplex-android-review.googlesource.com/c/platform/external/rust/crates/csv/+/27144932 Change-Id: I5b75cbbc3a7ac0267cd1c47559873b8378ea1574 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.
To bring this crate into your repository, either add csv
to your Cargo.toml
, or run cargo add csv
.
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