34

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;
    }
    console.log(data + 'my data')
});

CONSOLE:

ID
D11
D33
D55

Here I want to get the elements in the column ID and store those in an array. How can I do that? Can anyone suggest me help. Thanks. My controller:

var partnersModel = new partners(params);
        fs.readFile(config.csvUploadPath, function read(err, data) {
            if (err) {
                throw err;
            }
        dataArray = data.toString().split(/\r?\n/);
            dataArray.forEach(function(v,i){
                if(v !== 'DUI'){
                  partnersModel.dui.push(v);
                }
            });
        });
        partnersModel.save(function(error, response){
Talha Awan
  • 4,573
  • 4
  • 25
  • 40
MMR
  • 2,869
  • 13
  • 56
  • 110
  • 1
    http://stackoverflow.com/questions/23080413/nodejs-reading-csv-file – Himesh Suthar Jan 21 '17 at 07:52
  • Possible duplicate of [NodeJs reading csv file](http://stackoverflow.com/questions/23080413/nodejs-reading-csv-file) – Chandan Rai Jan 21 '17 at 07:56
  • Is this actually a CSV file, or just a file containing a list of IDs? This would be pretty easy to parse without a library, but if you are parsing true CSV definitely use one! My module of choice is: https://www.npmjs.com/package/csv – le3th4x0rbot Jan 21 '17 at 08:06
  • Hi.Bailsey ihave only one column name ID and want to store all the values of it in an array. – MMR Jan 21 '17 at 08:08
  • This is not really CSV then, it is just a file with data on each row. You could use `data.split('\n')` [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split]. It would return an array like you desire. – le3th4x0rbot Jan 21 '17 at 08:28
  • partnersModel.save is being called before any of the file reading happens. Use `async.waterfall` or something. https://www.npmjs.com/package/async – le3th4x0rbot Jan 21 '17 at 18:57
  • you can try this library : https://www.npmjs.com/package/csv-grape – Nihar Sarkar Jul 04 '23 at 07:04

3 Answers3

52

Use a library, CSV has lots of gotchas. I have come to enjoy the package csv. It is located here: https://www.npmjs.com/package/csv . Here is a very quick example using the async api.

const fs = require('fs')
var parse = require('csv-parse')
fs.readFile(inputPath, function (err, fileData) {
  parse(fileData, {columns: false, trim: true}, function(err, rows) {
    // Your CSV data is in an array of arrys passed to this callback as rows.
  })
})

Since your file does not have multiple values per row and contains no delimiters besides newline, it is only trivially CSV. Maybe String.prototype.split() is for you?

const fs = require('fs')
fs.readFile(inputPath, 'utf8', function (err, data) {
  var dataArray = data.split(/\r?\n/);  //Be careful if you are in a \r\n world...
  // Your array contains ['ID', 'D11', ... ]
})
Mat
  • 82,161
  • 34
  • 89
  • 109
le3th4x0rbot
  • 2,493
  • 23
  • 32
  • CSV files typically have multiple values per row, so it returns an array of arrays... one for each row. For your example `rows= [ ['id'], ['D11'], ['D33'], ['D55'] ]`. Note that the header was parsed too, and that to index one of the values you would need to use something like `rows[1][0]`. – le3th4x0rbot Jan 21 '17 at 08:24
  • There are options about header handling, and the library can help you stuff the values into objects for each row with properties matching the column names. TFM is located here: http://csv.adaltas.com/parse/ – le3th4x0rbot Jan 21 '17 at 08:25
  • But i am getting the error' data.split is not a function' – MMR Jan 21 '17 at 11:09
  • I thought `readFile` always passed a string as data, but apparently it only does if you specify an encoding. The example has been updated! – le3th4x0rbot Jan 21 '17 at 11:14
  • i got it by this... dataArray = data.toString().split(/\r?\n/); – MMR Jan 21 '17 at 11:22
  • In your example using 'csv-parse' I do not understand how you access the data in the end? – newandlost Aug 30 '21 at 14:31
  • @newandlost The sample is from before promises were a big thing, so it uses callbacks. Basically there is an anonymous function defined at the innermost level that node-csv calls with your parsed rows in an array. If you are still working on this I would be glad to help with an async/await version :) – le3th4x0rbot Sep 07 '21 at 11:06
17

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);
  })
ehacinom
  • 8,070
  • 7
  • 43
  • 65
  • 1
    How do I access the data in the array? for some reason (i think it because of the async nature of createReadStream) the array lentght equals 0, when I try to check it –  Jun 14 '22 at 11:49
  • parse is not defined. – Kyle Pennell Nov 26 '22 at 19:19
0

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<len; i++) {
      console.log(data[i]); //Will print every csv line as a newline
    }
  });
});

code snippet from jquery-csv's examples here.

Ahti Ahde
  • 1,078
  • 10
  • 12
zelusp
  • 3,500
  • 3
  • 31
  • 65