mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-21 19:13:07 +00:00
Add example: "Filter csv records matching a predicate".
This commit is contained in:
parent
d059872395
commit
588e7cb7f3
2 changed files with 56 additions and 0 deletions
|
@ -12,6 +12,7 @@
|
|||
| [Serialize records to CSV using Serde][ex-csv-serde] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] |
|
||||
| [Handle invalid CSV data with Serde][ex-invalid-csv] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] |
|
||||
| [Read CSV records with different delimeter][ex-csv-delimiter] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] |
|
||||
| [Filter CSV records matching a predicate][ex-csv-filter] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] |
|
||||
|
||||
[ex-json-value]: #ex-json-value
|
||||
<a name="ex-json-value"></a>
|
||||
|
@ -543,6 +544,59 @@ Ashley-Zurich-92";
|
|||
# quick_main!(run);
|
||||
```
|
||||
|
||||
[ex-csv-filter]: #ex-csv-filter
|
||||
<a name="ex-csv-filter"></a>
|
||||
## Filter CSV records matching a predicate
|
||||
|
||||
[![csv-badge]][csv] [![cat-encoding-badge]][cat-encoding]
|
||||
|
||||
|
||||
Returns _only_ the rows from `data` with a field that matches `query`.
|
||||
|
||||
```rust
|
||||
# #[macro_use]
|
||||
# extern crate error_chain;
|
||||
extern crate csv;
|
||||
|
||||
use std::io;
|
||||
#
|
||||
# error_chain!{
|
||||
# foreign_links {
|
||||
# Io(std::io::Error);
|
||||
# CsvError(csv::Error); // or just Seek(csv::Error)
|
||||
# }
|
||||
# }
|
||||
|
||||
fn run() -> Result<()> {
|
||||
let query = "CA";
|
||||
let data = "\
|
||||
City,State,Population,Latitude,Longitude
|
||||
Kenai,AK,7610,60.5544444,-151.2583333
|
||||
Oakman,AL,,33.7133333,-87.3886111
|
||||
Sandfort,AL,,32.3380556,-85.2233333
|
||||
West Hollywood,CA,37031,34.0900000,-118.3608333";
|
||||
|
||||
let mut rdr = csv::ReaderBuilder::new().from_reader(data.as_bytes());
|
||||
let mut wtr = csv::Writer::from_writer(io::stdout());
|
||||
|
||||
wtr.write_record(rdr.headers()?)?;
|
||||
|
||||
for result in rdr.records() {
|
||||
let record = result?;
|
||||
if record.iter().any(|field| field == query) {
|
||||
wtr.write_record(&record)?;
|
||||
}
|
||||
}
|
||||
|
||||
wtr.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
#
|
||||
# quick_main!(run);
|
||||
```
|
||||
|
||||
_Disclaimer: this example has been adapted from [the csv crate tutorial](https://docs.rs/csv/*/csv/tutorial/index.html#filter-by-search)_.
|
||||
|
||||
<!-- Categories -->
|
||||
|
||||
[cat-encoding-badge]: https://badge-cache.kominick.com/badge/encoding--x.svg?style=social
|
||||
|
|
|
@ -53,6 +53,7 @@ community. It needs and welcomes help. For details see
|
|||
| [Serialize records to CSV using Serde][ex-csv-serde] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] |
|
||||
| [Handle invalid CSV data with Serde][ex-invalid-csv] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] |
|
||||
| [Read CSV records with different delimeter][ex-csv-delimiter] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] |
|
||||
| [Filter CSV records matching a predicate][ex-csv-filter] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] |
|
||||
|
||||
## [Concurrency](concurrency.html)
|
||||
|
||||
|
@ -302,3 +303,4 @@ Keep lines sorted.
|
|||
[ex-urlencoded]: encoding.html#ex-urlencoded
|
||||
[ex-random-file-access]: basics.html#ex-random-file-access
|
||||
[ex-regex-replace-named]: basics.html#ex-regex-replace-named
|
||||
[ex-csv-filter]: encoding.html#ex-csv-filter
|
||||
|
|
Loading…
Reference in a new issue