简体   繁体   English

将.data文件导入D3js?

[英]Importing a .data file into D3js?

So I've been given the task of importing some data to visualize. 因此,我承担了导入一些数据以进行可视化的任务。 This to be specific: 具体来说:

http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data

However, I have not a clue on how to import a .data file. 但是,我对如何导入.data文件一无所知。 Furthermore, there is no header line in order to actually organise the file. 此外,没有标题行可以实际组织文件。

I have tried to import as text and the use csv.parseRows() which led to arrays with a length of one just containing one long string: 我试图导入为文本,并使用csv.parseRows()导致长度为1的数组仅包含一个长字符串:

I have tried to import as d3.tsv(). 我试图导入为d3.tsv()。 Every technique ends up importing a row as one long string like below. 每种技术最终都将一行作为一个长字符串导入,如下所示。

    "18.0   8   307.0      130.0      3504.      12.0   70  1   "chevrolet chevelle malibu""

I have never dealt with .data files and resources on using them with D3 are scarce. 我从来没有处理过.data文件,而且在D3上使用它们的资源很稀缺。 Any help on dealing with this would be greatly appreciated as always. 与往常一样,在处理此问题方面的任何帮助将不胜感激。 You people are awesome. 你们真棒。

Thanks. 谢谢。

You could use a regular expression to parse the string: 您可以使用正则表达式来解析字符串:

/("[^"]+"|[^\s]+)\s*/g

With this regular expression, you turn 使用此正则表达式,您可以

18.0   8   307.0      130.0      3504.      12.0   70  1    "chevrolet chevelle malibu"

into 进入

[
  "18.0",
  "8",
  "307.0",
  "130.0",
  "3504.",
  "12.0",
  "70",
  "1",
  "\"chevrolet chevelle malibu\""
]

You can do this for every line by splitting the contents of the data file by each newline, and then mapping each line to the regular expression: 您可以通过每行换行分割数据文件的内容,然后将每行映射到正则表达式来对每一行执行此操作:

d3.text("http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data", function(data) {
    var lines = data.split('\n');
    var results = data.map(function (d) {
      var match = d.match(regex);
      return match.slice(1).map(function (d) { return d.trim(); });
    });
});

Here is a quick fiddle showing this applied on one row: http://jsfiddle.net/tftdepL4/1/ 这是快速的小提琴,将其显示在一行上: http : //jsfiddle.net/tftdepL4/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM