How to read csv file in node js

前端 未结 3 1581
星月不相逢
星月不相逢 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 10:51

    From How to read data From *.CSV file using javascript?, use the jQuery-CSV library.

    Note: The library is designed to handle any CSV data that is RFC 4180 compliant, including all of the nasty edge cases that most 'simple' solutions overlook.

    var fs = require('fs');
    var $ = jQuery = require('jquery');
    $.csv = require('jquery-csv');
    
    var sample = './path/to/data/sample.csv';
    fs.readFile(sample, 'UTF-8', function(err, csv) {
      $.csv.toArrays(csv, {}, function(err, data) {
        for(var i=0, len=data.length; i

    code snippet from jquery-csv's examples here.

提交回复
热议问题