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

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

问题描述

我有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 加载数据,然后

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

d3.text("data/testnh.csv", function(text) {
  console.log(d3.csv.parseRows(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.csv.parseRows(text).map(function(row) {
    return row.map(function(value) {
      return +value;
    });
  });
  console.log(data);
});

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

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