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:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@bjdooi thanks for stopping in. Change your
data
handler to: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.This really should be sorted out as I wasted a good amount of time using the example code on npmjs.