Modifying csv data

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()

    for result in rdr.records() {
         let record: Record = result.unwrap().deserialize(None).unwrap();
      // if is 'x' then set to 'y'

fn main() {
    if let Err(err) = example() {
        println!("error running example: {}", err);

This was answered here:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.