Last active
April 24, 2024 02:05
-
-
Save iamnivekx/bde257d1af99dfd490ff25f963921c41 to your computer and use it in GitHub Desktop.
node csv example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
// const csv = require('csv'); | |
const { stringify } = require('csv-stringify'); | |
const { parse } = require('csv-parse'); | |
const { transform } = require('stream-transform'); | |
async function readFromCSVFile(filename = 'read.csv') { | |
const readStream = fs.createReadStream(filename); | |
const rows = []; | |
const parser = parse({ | |
columns: true, | |
skip_empty_lines: true | |
}); | |
return new Promise((resolve, reject) => { | |
readStream.pipe(parser) | |
.on("data", function (row) { | |
rows.push(row); | |
}).on("end", function () { | |
resolve(rows); | |
}).on("error", function (error) { | |
reject(error); | |
}); | |
}); | |
} | |
async function writeToCSVFile(data = [], filename = 'write.csv') { | |
const writeStream = fs.createWriteStream(filename); | |
const columns = ['row', 'value']; | |
const stringifer = stringify({ header: true, columns: columns }); | |
for (const row of data) { | |
stringifer.write(row); | |
} | |
stringifer.pipe(writeStream); | |
} | |
async function transformCSV(readFilename = 'read.csv', writeFilename = 'write.csv') { | |
const reader = fs.createWriteStream(readFilename); | |
const writeStream = fs.createWriteStream(writeFilename); | |
return reader | |
.pipe(parse({ columns: true })) | |
.pipe(transform((input) => { | |
return Object.assign({}, input, { | |
value: +input['value'] + 1 | |
}); | |
})) | |
.pipe(stringify({ header: true })) | |
.pipe(writeStream) | |
.on('finish', () => { | |
console.log('Done 🍻 '); | |
}); | |
} | |
async function main() { | |
const records = new Array(100).fill(0).map((_, i) => ({ row: i, value: i })); | |
const filename = '1.csv'; | |
await writeToCSVFile(records.map((item) => [item.row, item.value]), filename); | |
console.log('Read csv file firstly : ', await readCSVFile(filename)); | |
transformCSV(filename, '2.csv'); | |
readRecords = await readFromCSVFile(filename); | |
console.log('Read csv file secondly : ', await readFromCSVFile(filename)); | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment