D3: ignore certain row in .csv

前端 未结 2 1154
轮回少年
轮回少年 2020-12-29 17:34

I have a .csv file that looks like:

The Start Date, The User Id, The User Name,
date, id, name,
12-12-12, 12345, John Doe
01-02-13, 67891, Jane Doe


        
相关标签:
2条回答
  • 2020-12-29 17:50

    The d3.csv() function does both the loading and the parsing without giving you a chance to intervene. Since you need to intervene, you'll want to load the csv as plain text –– without parsing it –– then remove the first line, and then pass it as a String into the csv module for parsing.

    For loading, use d3.xhr() For parsing, use d3.csv.parse().

    You should end up with something like this:

    d3.xhr('data.csv').get(function (err, response) {
      var dirtyCSV = response.responseText;
      var cleanCSV = dirtyCSV.split('\n').slice(1).join('\n');
      var parsedCSV = d3.csv.parse(cleanCSV);
    })
    

    Edit

    Here is a less costly transformation than the one above (this has not been tested/debugged):

    d3.xhr('data.csv').get(function (err, response) {
      var dirtyCSV = response.responseText;
      var firstEOL = dirtyCSV.indexOf('\n');
      var parsedCSV = d3.csv.parse(cleanCSV.substring(firstEOL+1));
    })
    
    0 讨论(0)
  • 2020-12-29 17:55

    For those who want to ignore certain rows from csv, following snippet can be used.

    //Read the data
    d3.csv("abc.csv?"+Math.random(),
      function(d){
        //if current row's value column is blank, then ignore it. Otherwise do further process.
        if(d.value!=''){ 
          return { date : d3.timeParse("%Y-%m-%d %H:%M:%S")(d.date), value : d.value }
        }
      },
    
    0 讨论(0)
提交回复
热议问题