37

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){
7

4 Answers 4

56

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', ... ]
})
Sign up to request clarification or add additional context in comments.

7 Comments

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].
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: csv.adaltas.com/parse
But i am getting the error' data.split is not a function'
I thought readFile always passed a string as data, but apparently it only does if you specify an encoding. The example has been updated!
i got it by this... dataArray = data.toString().split(/\r?\n/);
|
19

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);
  })

3 Comments

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
parse is not defined.
@KylePennell You have to const csv = require('csv-parse') which imports the whole module and then use the parse method of that with csv.parse(...
1

you can also use csv-for-you npm package

const csv=require("csv-for-you");
function readFile async(filePath){
  const lines = await csv.parse(filePath);
}
readFile("/path/to/your/file.csv");

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.