解析多个CSV文件中的特定列/数据 [英] Parsing specific columns/data from multiple CSV file

查看:245
本文介绍了解析多个CSV文件中的特定列/数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,刚刚我一直在使用D3.js来解析CSV文件中的数据。在阅读Scott Murray的交互式数据可视化(伟大的书和非常丰富的信息)时,它解释了如何从CSV中的表中选择所有数据。

So, just recently I have been using D3.js to parse data from a CSV file. While reading the "Interactive Data Visualization" by Scott Murray (great book and very informative) it explains how to select all data from a table in a CSV.

解析CSV如下所示:

d3.text("three.csv", function(data) {
            var parsedCSV = d3.csv.parseRows(data);
            var container = d3.select("#thirdCSV")  
                .append("div")

                .append("table")
                    .attr("id", "tableThree")

                .selectAll("tr")
                    .data(parsedCSV).enter()
                    .append("tr")

                .selectAll("td")
                    .data(function(d) { return d; }).enter()
                    .append("td")
                    .attr("id","three")
                    .text(function(d) { return d; });

    });

对于我的项目,我采取7个不同的CSV文件并将其放置为HTML格式。

For my project, I am taking 7 different CSV files and placing it on a HTML format.

编写7x以上的代码似乎是不必要的,有没有另一种方法来解析多个CSV文件?如果是,是否还有一种方法来解析只有特定的列,如列D-F?这是一个如何设置其中一个文件的图像(其余的CSV文件是相同的格式。

Writing the code above 7x seems unnecessary, is there another way to parse multiple CSV file? If so, is there also a way to parse only specific columns such as columns D-F? Here is an image of how one of the files is set up (the rest of the CSV files are the same format.

推荐答案

有一个特定的 d3.text( d3.csv())中的csv文件中读取nofollow ),因为它使事情变得更容易,因为你不需要使用 d3.csv.parseRows() http://bl.ocks.org/enjalot/1525346rel =nofollow>此块例如。

There is a specific function for reading in csv files in D3 d3.csv(). You should use it instead of d3.text() because it makes things much easier since you do not need to use d3.csv.parseRows() in addition. Look this block for example.

您可以使用 d3.queue() 函数用于异步加载几个你应该使用这样:

You can use d3.queue() function for asynchronous loading of several data files. It waits for all the files to be loaded before the next steps are taken. You should use it like this:

var q = d3.queue()
    .defer(d3.csv, "first.csv")
    .defer(d3.csv, "second.csv")
    .defer(d3.csv, "third.csv")
    ....
    .awaitAll(function(error, results) {
      if (error) throw error;
      console.log(results);
      draw(results)
    });

我还要指出 D3.queue code>函数不包括在D3 v3默认包中,因此您必须单独加载它:

I also want to point out that the D3.queue() function is not included in D3 v3 default bundle, so you have to load it separately:

<script src="https://d3js.org/d3-queue.v3.min.js"></script>

这篇关于解析多个CSV文件中的特定列/数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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