help,Write a program in Rust to read a CSV file and create two output CSV files – one file with odd rows and the other file with even rows from the input file

how to Write a program in Rust to read a CSV file and create two output CSV files – one file with odd rows and the other file with even rows from the input file.

Have you tried reading the file line by line while keeping a counter and checking if that counter is divisible by 2 ?

i was able to seprate the rows and save them into different vectors ,how to convert thses vectors into csv tabe format in rust

If the lines are already from a csv and you just want to copy them to other files, there's nothing to do, just write them as you found them. (you don't even need to store them in temporary Vecs)

extern crate csv;


// Import the standard library's I/O module so we can read from stdin.
use std::io;

// The `main` function is where your program starts executing.
fn main() {
    // Create a CSV parser that reads data from stdin.
    let mut a=Vec::new();
    let mut b=Vec::new();
    let mut rdr = csv::Reader::from_reader(io::stdin());
    // Loop over each record.
    for (index, result) in rdr.records().enumerate() {
        if index%2==0{
            a.push(result);
        }
        else{
            b.push(result);
        }
    }
    println!("{:?}",b);
    
    for result in rdr.records() {
        // An error may occur, so abort the program in an unfriendly way.
        // We will make this more friendly later!
        let record = result.expect("a CSV record");
        // Print a debug version of the record.
        println!("{:?}", record);
    }
    
}

these is the source prb
Write a program in Rust to read a CSV file and create two output CSV files – one file with odd rows and the other file with even rows from the input file.

You don't need to parse the original file. As you read the source file line by line, write the lines in the corresponding file.


https://users.rust-lang.org/t/forum-code-formatting-and-syntax-highlighting

1 Like

i need to seperate out the odd and even lines of the source file ,
can plz provide me an example
i am struggling a lot

can u plz provide the code

use std::{
    fs,
    io::{self, BufRead, BufReader, BufWriter, Write},
};

fn main() -> io::Result<()> {
    let input = BufReader::new(fs::File::open("input.csv")?);
    let mut even_output = BufWriter::new(fs::File::create("even_output.csv")?);
    let mut odd_output = BufWriter::new(fs::File::create("odd_output.csv")?);
    for (i, line) in input.lines().enumerate() {
        let line = line?;
        if i % 2 == 0 {
            writeln!(even_output, "{line}")?;
        } else {
            writeln!(odd_output, "{line}")?;
        }
    }
    Ok(())
}

I'm reluctant to just give it you since it seems like homework.

3 Likes

the csv file contains a table content with two columns and 20 rows

the output file also need to be a in that format

output file is coming out like this ,in a singe line
42208,Palavakkam99943,Jodhpur Park185438,Kalewadi Pandhapur Road123183,Airoli174961,

expected output:,should be like rows and columns:
42208,Palavakkam99943
185438,Jodhpur Park
Road123183,Airoli174961

can u provide a good source ,where i can learn rust from basic

Try to do the whole book.

You can keep the first 20 lines, copy them to both files and then do the split as written above.

not working same result ,the file is saving as a line
" Property_ID,locality90879,Manikonda113926,Chembur54308,East of Kailash51866,Saket134438,Dronagiri176174,Kharadi129834,Byculla16496,Chandkheda178891,Wakad73453,West End"

the even and odd csv should be

Property_ID,locality
42208,Palavakkam
90879,Manikonda
99943,Jodhpur Park
113926,Chembur
185438,Kalewadi Pandhapur Road
54308,East of Kailash
123183,Airoli
51866,Saket
174961,Pimple Gurav
134438,Dronagiri
108475,Powai
176174,Kharadi
163273,Kharghar
129834,Byculla
33147,Sivarampalli
16496,Chandkheda
126462,Bandra East
178891,Wakad
134638,Santacruz East
73453,West End
82612,Kukatpally

with the headings

use std::{
fs,
io::{self, BufRead, BufReader, BufWriter, Write},
};

fn main() -> io::Result<()> {
let input = BufReader::new(fs::File::open("ru_csv.csv")?);
let mut even_output = BufWriter::new(fs::File::create("even_output.csv")?);
let mut odd_output = BufWriter::new(fs::File::create("odd_output.csv")?);
for (i, line) in input.lines().enumerate() {
let line = line?;
if i % 2 == 0 {
write!(even_output, "\n{line}")?;
} else {
write!(odd_output, "\n{line}")?;
}
}
Ok(())
}