CSV parseRows

3,983 views
Skip to first unread message

r34ch

unread,
Jul 13, 2011, 12:23:33 PM7/13/11
to d3...@googlegroups.com
Hey everyone,

After some hicups with Chrome I managed to get a local CSV file read into my program with a little help from an example and a friend. Now I am trying to convert it into an array of arrays like specified in d3.csv.parseRows. So far I have this;


d3.csv("filename", f_csv);


function f_csv(csv)
{
var a_temp = d3.csv.parseRows(csv);
console.log(a_temp);
}

But I am getting an error in the d3.svg.js apparantly so I am finding it hard to track down the problem. What am I overlooking?
Cheers

Mike Bostock

unread,
Jul 13, 2011, 12:35:38 PM7/13/11
to d3...@googlegroups.com
You don't need to call d3.csv.parseRows when using d3.csv; d3.csv
parses the file for you. Example:

http://mbostock.github.com/d3/ex/calendar.html

Also see:

https://github.com/mbostock/d3/wiki/CSV

Mike

r34ch

unread,
Jul 14, 2011, 6:25:32 AM7/14/11
to d3...@googlegroups.com
Unless I have read it wrong, I thought d3.csv parses to an array of objects? Whereas I am looking for an array of arrays.

Jason Davies

unread,
Jul 14, 2011, 6:38:49 AM7/14/11
to d3...@googlegroups.com
On Thu, Jul 14, 2011 at 03:25:32AM -0700, r34ch wrote:
> Unless I have read it wrong, I thought d3.csv parses to an array of objects?
> Whereas I am looking for an array of arrays.

Yes, d3.csv is a helper that uses d3.csv.parse internally to parse CSV
data loaded from a URL into an array of objects, and then it passes this
to a callback. So your example code wouldn't work because your callback
would be called with an array of objects, and calling d3.csv.parseRows
wouldn't work on this array.

If you want to parse into an array of arrays, then you should do:

d3.text(url, "text/csv", function(text) {
var rows = d3.csv.parseRows(text);
// Do stuff with rows here...
});

In fact, this boils down to replacing "d3.csv(...)" with "d3.text(...)"
(with "text/csv") in your example, so you were almost there. :)

Hope that helps,
--
Jason Davies, http://www.jasondavies.com/

Message has been deleted

r34ch

unread,
Jul 14, 2011, 7:18:53 AM7/14/11
to d3...@googlegroups.com
Thanks guys!! Fast and great responses as usual, can't thank you enough for putting up with the newbie questions :)
For anyone else struggling, my final code is:


d3.text("csvfile", "text/csv", f_csv);
function f_csv(csv)
{
var rows = d3.csv.parseRows(csv);
a_data = rows;
}


Cheers

Prabu R

unread,
Aug 12, 2015, 10:32:32 AM8/12/15
to d3-js
Hi Frieds,

I need to assign this parsed array into an global variable. Is it possible? If yes, Could anyone please help me. Thanks in advance.

Mike Tahani

unread,
Aug 13, 2015, 8:47:50 PM8/13/15
to d3...@googlegroups.com
You probably want to just use d3.csv, like so:

var globalRows;
d3.csv('file.csv', function (err, rows) {
  if (err) throw err;
  
  globalRows = rows;
});

Since d3.csv is async, you're not going to be able to work with globalRows until the callback is executed, so you'll probably want to operate on the data inside the callback (or pass it along to another function) instead of assigning it to a global variable.


--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages