7

Right now I am trying with the library fast-csv doing this:

var stream = fs.createReadStream("./google.csv");
  csv
   .fromStream(stream, {headers : ["Name","E-mail 1 - Value"], ignoreEmpty: true})
   .on("data", function(data){
       console.log(data);
   })
   .on("end", function(){
       console.log("done");
   });

But it throws this error: "column header mismatch expected: 2 columns got: 57"

Do you know how can I avoid that? should I use a different library/approach

Another problem I am facing is that I get the result in hexadecimal... how can I parse it correctly?

1
  • Remove the headers property from the second argument. That CSV clearly has 57 columns and you only provided the headers for two of them. Commented Oct 29, 2014 at 23:52

1 Answer 1

7

You can use node module 'csv-parse'.

  1. read the csv file using node 'fs' module
  2. pass the data to csv parser and you will get arry of array, where inner array represents each row.

Take a look at the following code.

var csvParser = require('csv-parse');

fs.readFile(filePath, {
  encoding: 'utf-8'
}, function(err, csvData) {
  if (err) {
    console.log(err);
  }

  csvParser(csvData, {
    delimiter: ','
  }, function(err, data) {
    if (err) {
      console.log(err);
    } else {
      console.log(data);
    }
  });
});

Here filePath is path of the csv file and the delimiter will be as per your file. It is the character that separates fields in csv file(can be ',', '.', etc).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Techxxx, this approach worked better, but now I am facing this issue: everything is hexadecimal, how would you deal with that?
@Cedric Sorry, but I don't understand your problem. If you explain it then I can try to help you.

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.