Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cookbook example for whitespace #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/cookbook-read-whitespace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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::ReaderBuilder::new()
.trim(csv::Trim::All)
.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);
}
}
11 changes: 11 additions & 0 deletions examples/data/smallpop-whitespace.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
city, region, country, population
Southborough, MA, United States, 9686
Northbridge, MA, United States, 14061
Westborough, MA, United States, 29313
Marlborough, MA, United States, 38334
Springfield, MA, United States, 152227
Springfield, MO, United States, 150443
Springfield, NJ, United States, 14976
Springfield, OH, United States, 64325
Springfield, OR, United States, 56032
Concord, NH, United States, 42605
41 changes: 41 additions & 0 deletions src/cookbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ For **reading** CSV:
2. [With Serde](#reading-with-serde)
3. [Setting a different delimiter](#reading-setting-a-different-delimiter)
4. [Without headers](#reading-without-headers)
5. [With whitspace](#reading-with-whitespace)

For **writing** CSV:

Expand Down Expand Up @@ -195,6 +196,46 @@ $ cd rust-csv
$ cargo run --example cookbook-read-no-headers < examples/data/smallpop-no-headers.csv
```

# Reading: with whitespace

This example shows how to read CSV data seperated by whitespace as well as a comma.

```no_run
# //cookbook-read-whitespace.rs
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::ReaderBuilder::new()
.trim(csv::Trim::All)
.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:

```ignore
$ git clone git://github.com/BurntSushi/rust-csv
$ cd rust-csv
$ cargo run --example cookbook-read-whitespace < examples/data/smallpop-whitespace.csv
```

# Writing: basic

This example shows how to write CSV data to stdout.
Expand Down