TransWikia.com

How to parse CSV with node.js?

Stack Overflow Asked by idarosa on December 26, 2020

I’m trying to follow an example of a streamgraphs in highcharts, link, but it says "parsed with node.js" instead of giving out the file/explaining how to do it.

I need to go from a csv formatted like this:

Category, Finland, Austria
1,        53,      29
2,        77,      88

To this in my js file:

series: [{
    name: "Finland",
    data: [
      53, 77
    ]
  }, {
    name: "Austria",
    data: [
      29, 88
    ]
  }, 

EDIT: but how do I actually merge it with the javascript script in highcharts (here)?

2 Answers

EDIT FOR OP: series: [...] on line 135 of the js in the codepen can be replaced by

series: result

you already know how result is calculated ;)

EDIT: the question was also answered by user F.NiX, just before this one

think along with me

the csv has a header and the rest is the data. so to make it easier to work with, let us seperate those first by reading the file

const fs = require("fs")
const data = fs.readFileSync("path/to/your/file.csv", "utf-8")
const lines = data.toString().split('n')

and grabbing the header first, as it defines the structure of the output

const header = lines[0].replace('r', '')
// The replace is used to remove unwanted characters from the csv

this leaves us with the rest of the entries left as

const entries = lines.slice(1) // everything after the header

now for the final part: combining it all in the format you proposed, we have a list

const result = []

that has objects with the name of the country, gotten from the header, and data which we will fill with the entries later

header.split(', ').slice(1).forEach((name) => { // the slice is to skip Category
  result.push({ name, data: []})
})

each object has the entries by column, so we spread the numbers across the objects for each line

entries.forEach(line => {
  line.replace('r', '').split(', ').slice(1).forEach((number, column) => {
    result[column].data.push(number)
  })
});

done, now do whatever you want with result

Answered by Raymonzut on December 26, 2020

Try this:

const { readFileSync } = require('fs')
let csv = readFileSync('path/to/your/file.csv', 'utf-8')
const series = []
csv = csv.split('n')
let headers = csv.shift().split(',')
headers.shift()
for (let i = 0; i < headers.length; i++) {
  const data = []
  for (let j = 0; j < csv.length; j++)
    data.push(csv[j].split(',')[i + 1].trim())
  csv[i].split(',')
  series.push({
    name: headers[i].trim(),
    data
  })
}
console.log(series)

Answered by F.NiX on December 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP