minimalpop
minimalpop

Reputation: 7058

D3: ignore certain row in .csv

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

The first row are descriptions for the keys on the second row. I'm loading in the .csv file as follows:

d3.csv("data.csv", function(error, data) {
  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.id   = formateID(d.id);
    d.name = formatename(d.name);
    d.close = +d.close;
  });

I want to keep the first row (description) separate from my data model because I still plan on using it down the road, however I'm not allowed to modify the format of the .csv file. Is there a way I can only capture this information:

date, id, name,
12-12-12, 12345, John Doe
01-02-13, 67891, Jane Doe

from the csv?

Upvotes: 3

Views: 5779

Answers (2)

yılmaz
yılmaz

Reputation: 1826

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 }
    }
  },

Upvotes: 0

meetamit
meetamit

Reputation: 25157

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));
})

Upvotes: 11

Related Questions