in

Does csv-parse allow you to read from file?


I’m learning how to use the csv-parse module for nodejs. I wrote this code and it works perfectly:

var fs = require('fs');
  
const fileName="./spreadsheet.csv";
const assert = require('assert');
const { parse } = require('csv-parse');

const records = [];
// Initialize the parser
const parser = parse({
  delimiter: ','
});
// Use the readable stream api to consume records
parser.on('readable', function(){
  let record;
  while ((record = parser.read()) !== null) {
    records.push(record);
  }
});
// Catch any error
parser.on('error', function(err){
  console.error(err.message);
});


fs.readFile(fileName, 'utf8', function (err, f) {
   if (err) {
      return console.error(err);
   }
   const rows = f.split("rn");
   
   for(let x in rows) {
       parser.write(rows[x]+"n");
   }
   parser.end();

   console.log(records);
});

But right now, I depend on the fs module and fs.readFile to consume my csv file. Does the csv-parse have an option to read ffrom file? I ask because as you can see in my code, I ahve to specify my own line-break characters, which could differ between csv files. I thought maybe the csv-parse module would have something that can more readily address such a situation?



Source: https://stackoverflow.com/questions/70556960/does-csv-parse-allow-you-to-read-from-file

IN MongoDB Query

Realtime YOLO Monster Detection With Non Maximum Supression