Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/BurntSushi/rust-csv
A CSV parser for Rust, with Serde support.
https://github.com/BurntSushi/rust-csv
csv library rust rust-library
Last synced: 7 days ago
JSON representation
A CSV parser for Rust, with Serde support.
- Host: GitHub
- URL: https://github.com/BurntSushi/rust-csv
- Owner: BurntSushi
- License: unlicense
- Created: 2014-03-22T06:58:51.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-11-23T21:15:58.000Z (12 months ago)
- Last Synced: 2024-01-31T10:13:36.700Z (9 months ago)
- Topics: csv, library, rust, rust-library
- Language: Rust
- Homepage:
- Size: 2.87 MB
- Stars: 1,563
- Watchers: 16
- Forks: 209
- Open Issues: 80
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
- awesome-rust-cn - BurntSushi/rust-csv - ci.org/BurntSushi/rust-csv.svg?branch=master">](https://travis-ci.org/BurntSushi/rust-csv) (Libraries / Encoding)
- awesome-rust - BurntSushi/rust-csv - ci.org/BurntSushi/rust-csv.svg?branch=master">](https://travis-ci.org/BurntSushi/rust-csv) (Libraries / Encoding)
- awesome-rust - BurntSushi/rust-csv - ci.org/BurntSushi/rust-csv.svg?branch=master">](https://travis-ci.org/BurntSushi/rust-csv) (代码 / 编码)
- awesome-rust - BurntSushi/rust-csv
- awesome-rust-cn - BurntSushi/rust-csv
- awesome-rust-zh - BurntSushi/rust-csv - 快速灵活的 CSV 读写器,支持 Serde[<img src="https://api.travis-ci.org/BurntSushi/rust-csv.svg?branch=master">](https://travis-ci.org/BurntSushi/rust-csv) (库 / 编码(Encoding))
- awesome-rust - BurntSushi/rust-csv - A fast and flexible CSV reader and writer, with support for Serde (Libraries / Encoding)
- awesome-rust - BurntSushi/rust-csv - ci.org/BurntSushi/rust-csv.svg?branch=master">](https://travis-ci.org/BurntSushi/rust-csv) (库 Libraries / 加密 Encoding)
- awesome-rust-list - BurntSushi/rust-csv - csv?style=social"/> : A fast and flexible CSV reader and writer for Rust, with support for Serde. (File Processing)
- awesome-rust-list - BurntSushi/rust-csv - csv?style=social"/> : A fast and flexible CSV reader and writer for Rust, with support for Serde. (File Processing)
- fucking-awesome-rust - BurntSushi/rust-csv - A fast and flexible CSV reader and writer, with support for Serde (Libraries / Encoding)
- fucking-awesome-rust - BurntSushi/rust-csv - A fast and flexible CSV reader and writer, with support for Serde (Libraries / Encoding)
README
csv
===
A fast and flexible CSV reader and writer for Rust, with support for Serde.[![Build status](https://github.com/BurntSushi/rust-csv/workflows/ci/badge.svg)](https://github.com/BurntSushi/rust-csv/actions)
[![crates.io](https://img.shields.io/crates/v/csv.svg)](https://crates.io/crates/csv)Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).
### Documentation
https://docs.rs/csv
If you're new to Rust, the
[tutorial](https://docs.rs/csv/1.*/csv/tutorial/index.html)
is a good place to start.### Usage
To bring this crate into your repository, either add `csv` to your
`Cargo.toml`, or run `cargo add csv`.### Example
This example shows how to read CSV data from stdin and print each record to
stdout.There are more examples in the
[cookbook](https://docs.rs/csv/1.*/csv/cookbook/index.html).```rust
use std::{error::Error, io, process};fn example() -> Result<(), Box> {
// 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, 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:
```text
$ 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.```rust
use std::{error::Error, io, process};#[derive(Debug, serde::Deserialize)]
struct Record {
city: String,
region: String,
country: String,
population: Option,
}fn example() -> Result<(), Box> {
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
```