question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSVs aren't being parsed correctly as per the documentation

See original GitHub issue
  • Operating System: OS X El Capitan 10.11.6
  • Node Version: v11.0.0
  • NPM Version: 6.4.1
  • csv-parser Version: 2.1.0

Expected Behavior

Following the example in the README.md:

Suppose you have a CSV file data.csv which contains the data:

NAME, AGE
Daffy Duck, 24
Bugs Bunny, 22

It could then be parsed, and results shown like so:

const csv = require('csv-parser')
const fs = require('fs')
const results = [];

fs.createReadStream('data.csv')
  .pipe(csv())
  .on('data', results.push)
  .on('end', () => {
    console.log(results);
    // [
    //   { NAME: 'Daffy Duck', AGE: 24 },
    //   { NAME: 'Bugs Bunny', AGE: 22 }
    // ]
  });

Actual Behavior

I created a file data.csv in the same directory as the node file I’m running.

My code is copied and pasted from above:

const csv = require('csv-parser')
const fs = require('fs')
const results = [];

fs.createReadStream('data.csv')
  .pipe(csv())
  .on('data', results.push)
  .on('end', () => {
    console.log(results);
    // [
    //   { NAME: 'Daffy Duck', AGE: 24 },
    //   { NAME: 'Bugs Bunny', AGE: 22 }
    // ]
  });

However the result to console.log is: []

I know I’m accessing the correct thing because I then modified the code to this:

const csv = require('csv-parser')
const fs = require('fs')
const results = [];

fs.createReadStream('data.csv')
  .pipe(csv())
  .on('data', (results) => {
    console.log(results);
  })
  .on('end', () => {
    console.log(results);
    // [
    //   { NAME: 'Daffy Duck', AGE: 24 },
    //   { NAME: 'Bugs Bunny', AGE: 22 }
    // ]
  });

Output to console.log:

Row { NAME: 'Daffy Duck', ' AGE': ' 24' }
Row { NAME: 'Bugs Bunny', ' AGE': ' 22' }
[]

The other header is getting turned into a string value and it’s not being picked up. The empty array still persists

How Do We Reproduce?

Run the example

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
shellscapecommented, Oct 27, 2018

@bjdooi thanks for stopping in. Change your data handler to:

  .on('data', (d) => {
    results.push(d);
  })

Probably a context issue passing results.push as the handler fn. Feel free to submit a PR to the README to update this if you’re up for it.

0reactions
DanielAshercommented, Feb 24, 2019

This really should be sorted out as I wasted a good amount of time using the example code on npmjs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 most common parsing errors in CSV files (and ... - Medium
#2 — Text field with an unescaped delimiter​​ If the column separator appears unescaped in a text field, this will cause the line...
Read more >
6 Common CSV Import Errors and How to Fix Them - Flatfile
One of the most common CSV import errors is that the file is simply too large. That can be caused by too many...
Read more >
Resolving common problems with CSV files
In a .csv file, a table of values is stored with columns delimited by commas, and with line breaks separating rows. The .csv...
Read more >
My CSV/Excel file is formatted incorrectly
My CSV/Excel file is formatted incorrectly · 1. Open a blank workbook in Microsoft Excel. · 2. Click on File > Import. ·...
Read more >
I have trouble opening CSV files with Microsoft Excel. Is there ...
The Problem · The standard field delimiters for CSV files are commas: , · On American Windows versions, the comma is set as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found