I am trying to parse a CSV file and if a certain field matches, update a certain field with a different value, but I'm not sure on how to do this.
My code:
extern crate csv;
use std::error::Error;
fn run(file: &str, output: &str) -> Result<(), Box<Error>> {
let mut rdr = csv::Reader::from_path(file)?;
let mut wtr = csv::Writer::from_path(output)?;
wtr.write_record(rdr.byte_headers()?);
for result in rdr.byte_records() {
let mut record = result?;
if &record[0] == "05V".as_bytes() && &record[4] == "4".as_bytes() {
// let record[4] = "2"; -> Not sure how to update the field
}
wtr.write_byte_record(&record);
}
Ok(())
}
How can I update the field if the record matches the conditions?