javascript - Importing a .data file into D3js? -
so i've been given task of importing data visualize. specific:
http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data
however, have not clue on how import .data file. furthermore, there no header line in order organise file.
i have tried import text , use csv.parserows() led arrays length of 1 containing 1 long string:
i have tried import d3.tsv(). every technique ends importing row 1 long string below.
"18.0 8 307.0 130.0 3504. 12.0 70 1 "chevrolet chevelle malibu""
i have never dealt .data files , resources on using them d3 scarce. on dealing appreciated always. people awesome.
thanks.
you use regular expression parse string:
/("[^"]+"|[^\s]+)\s*/g
with regular expression, 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 every line splitting contents of data file each newline, , mapping each line 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 quick fiddle showing applied on 1 row: http://jsfiddle.net/tftdepl4/1/