sandbox-node/csv-parser/index.js

24 lines
684 B
JavaScript

const fs = require('fs');
const csv = require('csv-parser');
const parsedData = [];
// Create a read stream for the CSV file
const readStream = fs.createReadStream('210211_TicketSort_2021.csv');
// Pipe the read stream into the csv-parser stream
readStream
.pipe(csv())
.on('data', (row) => {
// Handle each row of parsed data here
parsedData.push(row);
})
.on('end', () => {
// All rows have been read and parsed
console.log('CSV file successfully processed');
console.log('Sample Parsed Data:', parsedData.slice(0, 5)); // Showing first 5 rows as a sample
})
.on('error', (error) => {
console.error('An error occurred:', error.message);
});