【问题标题】:How to parse CSV with node.js?如何使用 node.js 解析 CSV?
【发布时间】:2021-02-07 05:45:45
【问题描述】:

我正在尝试遵循 highcharts 中的流图示例 link,但它说“用 node.js 解析”而不是给出文件/解释如何做。

我需要从格式如下的 csv 中去:

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

在我的 js 文件中:

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

编辑:但我实际上如何将它与 highcharts (here) 中的 javascript 脚本合并?

【问题讨论】:

标签: javascript python node.js json pandas


【解决方案1】:

试试这个:

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)

【讨论】:

  • 谢谢,但究竟如何将它插入到我在这里找到的 js 文件中:highcharts.com/samples/highcharts/demo/streamgraph?codepen?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 2011-08-09
  • 2011-11-14
相关资源
最近更新 更多