How to read csv file in node js

前端 未结 3 1583
星月不相逢
星月不相逢 2021-01-17 10:18

I am trying to read a csv file using node js. Her is my code

fs.readFile(config.csvUploadPath, function read(err, data) {
    if (err) {
        throw err;
         


        
3条回答
  •  执念已碎
    2021-01-17 11:10

    I used a stream, fs, and csv-parse like in this answer:

    const parse = require('csv-parse')
    const fs = require('fs') 
    
    const data = []
    fs.createReadStream(filename)
      .pipe(parse({ delimiter: ',' }))
      .on('data', (r) => {
        console.log(r);
        data.push(r);        
      })
      .on('end', () => {
        console.log(data);
      })
    

提交回复
热议问题