Skip to content

Instantly share code, notes, and snippets.

@vbarbarosh
Last active June 21, 2019 19:05
Show Gist options
  • Save vbarbarosh/9523ea5dd540f717a912869c11051be8 to your computer and use it in GitHub Desktop.
Save vbarbarosh/9523ea5dd540f717a912869c11051be8 to your computer and use it in GitHub Desktop.
node_csv_parse – Parsing csv files and strings https://codescreens.com
const csv = require('neat-csv');
const fs = require('fs');
const path = require('path');
// Parsing csv files and strings
main().catch(panic);
// https://stackabuse.com/reading-and-writing-csv-files-with-node-js/
async function main()
{
const csv = `email,name
[email protected],Raffarty Willicott
[email protected],Ange O'Looney
[email protected],Bryna Sorey
[email protected],Loy Butfield`;
const filename = path.resolve(__dirname, '../input/users.csv');
console.log(await csv_parse_file(filename));
console.log(await csv_parse_stream(fs.createReadStream(filename)));
console.log(await csv_parse_string(csv));
}
function csv_parse_file(file)
{
return csv_parse_stream(fs.createReadStream(file));
}
function csv_parse_stream(stream)
{
return csv(stream);
}
function csv_parse_string(string)
{
return csv(string);
}
function panic(error)
{
console.error(error);
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment