Skip to content

Instantly share code, notes, and snippets.

@clakech
Created February 23, 2018 08:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clakech/7bb721fa659257e5a56b3bd9f937813a to your computer and use it in GitHub Desktop.
Save clakech/7bb721fa659257e5a56b3bd9f937813a to your computer and use it in GitHub Desktop.
parse stream csv file to create another csv file with only vanilla NodeJS (readline)
const readline = require('readline');
const fs = require('fs');
const uuid = require('uuid/v4');
const rl = readline.createInterface({
input: fs.createReadStream('./input.csv'),
output: fs.createWriteStream('./output.csv'),
});
rl
.on('line', function convert(line) {
rl.pause();
try {
const [firstname, lastname, hobby] = line.split(';');
this.output.write([uuid(), `${firstname} ${lastname}`, hobby, '\n'].join(';'));
rl.resume();
} catch (error) {
console.error('error handling line %j', line);
rl.resume();
}
})
.on('close', function onClose() {
console.log(`Successfully streamed`);
this.output.end();
});
@sventschui
Copy link

sventschui commented Feb 23, 2018

This .split is VERY dangerous. Take the following example, your code won't work:

const line = 'foo;"bar;baz";bacon';
const erroneous = line.split(";");
// erroneous: ['foo', '"bar', 'baz"', 'bacon']
// expected: ['foo', '"bar;baz"', 'bacon']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment