Parsing a CSV file using NodeJS

后端 未结 16 2318
执笔经年
执笔经年 2020-11-27 12:15

With nodejs I want to parse a .csv file of 10000 records and do some operation on each row. I tried using http://www.adaltas.com/projects/node-csv. I couldnt get this to pau

16条回答
  •  渐次进展
    2020-11-27 12:44

    I needed an async csv reader and originally tried @Pransh Tiwari's answer but couldn't get it working with await and util.promisify(). Eventually I came across node-csvtojson, which pretty much does the same as csv-parser, but with promises. Here is an example usage of csvtojson in action:

    const csvToJson = require('csvtojson');
    
    const processRecipients = async () => {
        const recipients = await csvToJson({
            trim:true
        }).fromFile('./recipients.csv');
    
        // Code executes after recipients are fully loaded.
        recipients.forEach((recipient) => {
            console.log(recipient.name, recipient.email);
        });
    };
    

提交回复
热议问题