Parsing CSV in NodeJS by last comma only

253 Views Asked by At

My team is trying to take questions by developers in a CSV and parse it to put into a database. Some developers have commas in their questions.

The format of the CSV is Question,Answer. For example, a row might be `Hi, how are you,good' or 'what is life?,42'

  fs.createReadStream(file)
    .pipe(createCsvParser())
    .on("data", (row) => {
      console.log(row);
      questionSet.push(row); //appends row to questionSet array
    })
    .on("end", () => {
      console.log("CSV file successfully processed");
    });

This is our current code. Thank you for your help

1

There are 1 best solutions below

0
On

Instead of writing your custom code. Consider using a CSV-Parse library. It is widely used and takes care of the situation you are describing. This is because the library uses CSV file headers to identify the data in rows.

After you install the library

const parse = require('csv-parse/lib/sync');

const fileContent = fs.readFileSync("/filpath/fileName.csv");

const questionSet = parse(fileContent, {
    columns: true,
    skip_empty_lines: true
});

The questionSet Array will have objects representing rows of your CSV with the situation you described being handled.