I found how to read
and how to write
csv file.
Id there a way, to change data in the file, in the below example, we are reading records, is there a way, for example to change the country
of a given field based on the city
?
use std::error::Error;
use std::io;
use std::process;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Record {
city: String,
region: String,
country: String,
population: Option<u64>,
}
fn example() -> Result<(), Box<dyn Error>> {
let file_path = Path::new("contacts.csv");
let mut rdr = csv::ReaderBuilder::new()
.has_headers(true)
.from_path(file_path).unwrap();
for result in rdr.records() {
let record: Record = result.unwrap().deserialize(None).unwrap();
// if record.city is 'x' then set record.country to 'y'
}
Ok(())
}
fn main() {
if let Err(err) = example() {
println!("error running example: {}", err);
process::exit(1);
}
}