Skip to content

Instantly share code, notes, and snippets.

@johnmurch
Created January 10, 2019 16:06
Show Gist options
  • Select an option

  • Save johnmurch/645d9e68679214665650a873eb832584 to your computer and use it in GitHub Desktop.

Select an option

Save johnmurch/645d9e68679214665650a873eb832584 to your computer and use it in GitHub Desktop.
Read a CSV file in Node.js
var fs = require('fs'),
readline = require('readline'),
stream = require('stream');
var instream = fs.createReadStream('./FILE.csv');
var outstream = new stream;
outstream.readable = true;
var isHeaders = true; // default - there exists a header, or swap true/false
var headers = [];
var rl = readline.createInterface({
input: instream,
output: outstream,
terminal: false
});
rl.on('line', function(line) {
// console.log(line);
// PARSING DATA
if(!isHeaders){
// if there are QUOTES(") seperating out the values
var csvLine = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
// append to file
//fs.appendFileSync('dump.txt',csvLine);
}
// PARSING HEADERS
if(isHeaders){
headers = line.split(',')
isHeaders = false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment