1

I need to read a CSV file, iterate over each row, create a new file of the row and delete the row. So far , i have done this way

    var fs = require("fs"); // module
    var glob = require("glob");// helps to find file from globally using path
    var path = require("path");

    glob.sync(path.join(__dirname, "Database.csv")).forEach(function(csv_filename) {
    if (!(/_new\.csv$/.test(csv_filename))) {
    fs.readFile(csv_filename, "utf8", function(err, data) {
        if (err){ // if there is some mistakes
            console.log(err)
        }else{
            let stringTotal="";
            var arrayEachLines = data.split("\n");
          /*  arrayEachLines.forEach(function(line){    
            stringTotal=stringTotal+line+"\n";
                            */
            var o={};

            var i;
            var row;
                for (i=0; i<arrayEachLines.length; i++){
                o[i]=arrayEachLines[i];
                o[i]=o[i].replace(o[row],"");

            }
            arrayEachLines.push(o);

            console.log(o);


            fs.writeFile(csv_filename.replace(/Excel\.csv_original/, "Excel.csv_updated").replace(/\.csv$/, "_new.csv"), o[i], "utf8", function(err) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(csv_filename + " converted");
                }
            });
        }

    });
}});

The database csv contains

  a,"""blue""",11,3
  ba,"""blue""",2,4
  c,"""blue""",0,5

I want the system to read first row, create a new file with the fields of first row, delete the row and read second row do the same action Any suggestion will be highly appreciated guys

Thank you

Bharat Paudyal
  • 124
  • 1
  • 10

1 Answers1

0

Create a event stream to emit each line using event-stream and write into new csv files. This will do the trick

const fs = require('fs');
const es = require('event-stream');
const path = require("path");

let lineCount = 0;
let csvPath = path.join(__dirname, "Database.csv");
fs.createReadStream(csvPath)
    .pipe(es.split())
    .pipe(es.mapSync((line) => {
        lineCount += 1;
        let filePath = path.join(__dirname, `line${lineCount}.csv`)
        fs.writeFileSync(filePath , line);
    }).on('error', (err) => {
        console.log('Error while reading file.', err);
    }).on('end', () => {
        console.log('Read entire file.')
    }));
kgangadhar
  • 4,886
  • 5
  • 36
  • 54