MAX GRIMM
MAX GRIMM

Reputation: 111

Parse CSV file and print the desired out put

Very new to nodejs. I have the following CSV file which is nothing but a report.

Label,# Samples,Average,Median,90% Line,95% Line,99% Line,Min,Max,Error %,Throughput,Received KB/sec,Std. Dev.
LAUNCH-APPLICATION,25,446,282,612,1425,2224,271,2224,0.00%,3.0,6.2,438.29
BOOKROOM,25,571,571,582,591,592,547,592,100.00%,3.9,5.9,11.44
TOTAL,50,508,561,591,612,2224,271,2224,50.00%,5.7,10.1,316.22

Want to print in this format, LAUNCH-APPLICATION instead of #samples I want to print No of Samples:25, Average:446 like this.

for example:

LAUNCH-APPLICATION: No of Samples:25,Average:446,95% Line:1425,99% Line:2224

BOOKROOM: No of Samples:25,Average:446,95% Line:1425,99% Line:2224

I have tried with

const stream = file.createReadStream('../ParsedReport/reportChange.csv')
const readlineFile = readLine.createInterface({ input: stream })

let data = [];

readlineFile.on("line", (row) => {
    data.push(row.split(","));
});
 
readlineFile.on("close", () => {
    //console.log(data);
    // starting from 1 to skip the first row and -2 for skipping the last two row.
    for(let j=1; j <= data.length-2 ; j++) {
        for(let i = 0; i < data[0].length; i++) {
            console.log(data[0][i],'-----------',data[j][i]);
       }
    }
    
});

Sample CSV file screenshot:

enter image description here

appreciate for help.

Upvotes: 0

Views: 42

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30675

I would suggest using a dedicated CSV parsing libary such as Papa Parse, this makes parsing such files very simple.

We'd create a formatRow function that takes a row object and formats it in the desired way:

const Papa = require('papaparse');
const fs = require("fs");

let csvData = fs.readFileSync('../ParsedReport/reportChange.csv', 'utf-8');
let { data } = Papa.parse(csvData, { header: true });

function formatRow(row) {
    return `${row.Label}: No of Samples: ${row['# Samples']}, Average: ${row.Average}, 95% Line: ${row['95% line']}, 99% Line:${row['99% Line']}`;
}

// We wish to exclude the first and last two lines... 
let requiredData = data.slice(1, data.length - 2);
for(let row of requiredData) {
    console.log(formatRow(row));
}

The output will look like:

LAUNCH-APPLICATION: No of Samples: 25, Average: 446, 95% Line: undefined, 99% Line:2224
BOOKROOM: No of Samples: 25, Average: 571, 95% Line: undefined, 99% Line:592
TOTAL: No of Samples: 50, Average: 508, 95% Line: undefined, 99% Line:2224

The data array will look like so:


    [
      {
        Label: 'LAUNCH-APPLICATION',
        '# Samples': '25',
        Average: '446',
        Median: '282',
        '90% Line': '612',
        '95% Line': '1425',
        '99% Line': '2224',
        Min: '271',
        Max: '2224',
        'Error %': '0.00%',
        Throughput: '3.0',
        'Received KB/sec': '6.2',
        'Std. Dev.': '438.29'
      },
      {
        Label: 'BOOKROOM',
        '# Samples': '25',
        Average: '571',
        Median: '571',
        '90% Line': '582',
        '95% Line': '591',
        '99% Line': '592',
        Min: '547',
        Max: '592',
        'Error %': '100.00%',
        Throughput: '3.9',
        'Received KB/sec': '5.9',
        'Std. Dev.': '11.44'
      },
   ]

Upvotes: 1

Related Questions