在 D3 中读取没有标题行的 csv/tsv [英] read csv/tsv with no header line in D3

查看:37
本文介绍了在 D3 中读取没有标题行的 csv/tsv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似于以下内容的 CSV 数据:

I have CSV data which looks something like:

1,1,10
1,2,50
1,3,5
etc...

我正在尝试读入数据.但是,我的初始数据不包含标题行(如上所示),因此将第一个数据行作为标题 (1,1,10).有没有办法解决.我想在读取数据后设置标题名称

And I am trying to read in the data. However, my initial data does not contain a header row (as seen above) so it is taking the first data row to be the header (1,1,10). Is there anyway around this. I want to set the header names after I read the data

d3.csv("data/testnh.csv", function(data) {
    console.log(data);
}

谢谢!

推荐答案

使用 d3.text 加载数据,然后 d3.csvParseRows 来解析它.例如:

Use d3.text to load the data, and then d3.csvParseRows to parse it. For example:

d3.text("data/testnh.csv", function(text) {
  console.log(d3.csvParseRows(text));
});

您可能还想将列转换为数字,因为默认情况下它们将是字符串.假设它们是所有数字,你可以说:

You'll probably also want to convert your columns to numbers, because they'll be strings by default. Assume they are all numbers, you could say:

d3.text("data/testnh.csv", function(text) {
  var data = d3.csvParseRows(text).map(function(row) {
    return row.map(function(value) {
      return +value;
    });
  });
  console.log(data);
});

这篇关于在 D3 中读取没有标题行的 csv/tsv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆