AnswerBun.com

How to parse CSV with node.js?

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)?

Stack Overflow Asked by idarosa on December 26, 2020

2 Answers

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!

Related Questions

onclick is not calling the javascript function

1  Asked on December 15, 2020 by umakanth-pendyala

   

Node-MySQL unable to insert TIME

1  Asked on December 15, 2020 by niclassic

         

How to annotate a value from a related model in Django

2  Asked on December 15, 2020 by roman-safonov

     

A network-related or instance-specific error with C#

1  Asked on December 15, 2020 by nathan-nguyen

 

Where are user deletion logs stored on SAP?

1  Asked on December 15, 2020 by jorge-valentini

   

How can I pass date/text to ?

2  Asked on December 15, 2020 by muska

   

Java Scanner useDelimiter() Method

1  Asked on December 14, 2020 by mnh

 

How to implement negative null floating point regex

2  Asked on December 14, 2020 by frontdev24

   

Cannot build Dockerfile

1  Asked on December 14, 2020 by user1765862

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.