admin管理员组文章数量:1419243
I have two versions of a horizontal bar chart, stacked and paired (aka grouped). The code for each is stuffed into their own functions.
Refer to lines 35-43 (var stacked = function (){ ... })
and lines 117-121 (var paired = function (){ ... })
to see how the CSV is currently being parsed:
Unment line 197 and below to see the stacked version rendered. To do this I manually changed the data, not ideal.
GOAL: I want to parse the csv file and drop a column programmatically, rather than remove it manually and save 2 separate csv files.
I need to parse the csv and remove this with native d3 or javascript code, but how?
Shown in code.
I have:
Category,Total,Under $1000, ...
Music,14744,1434, ...
Art,12796,1216, ...
I need total removed:
Category,Under $1000, ...
Music,1434, ...
Art,1216, ...
I have two versions of a horizontal bar chart, stacked and paired (aka grouped). The code for each is stuffed into their own functions.
Refer to lines 35-43 (var stacked = function (){ ... })
and lines 117-121 (var paired = function (){ ... })
to see how the CSV is currently being parsed: http://tributary.io/inlet/8832116
Unment line 197 and below to see the stacked version rendered. To do this I manually changed the data, not ideal.
GOAL: I want to parse the csv file and drop a column programmatically, rather than remove it manually and save 2 separate csv files.
I need to parse the csv and remove this with native d3 or javascript code, but how?
Shown in code.
I have:
Category,Total,Under $1000, ...
Music,14744,1434, ...
Art,12796,1216, ...
I need total removed:
Category,Under $1000, ...
Music,1434, ...
Art,1216, ...
Share
Improve this question
edited Nov 16, 2015 at 23:20
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Feb 12, 2014 at 1:14
DeBraidDeBraid
9,4415 gold badges33 silver badges43 bronze badges
4 Answers
Reset to default 2If you have the CSV string in memory (a variable string), split on \n
to get the lines, loop over each line and split on ,
. This will give you an array of the column values for a given line.
Splice out the column you don't want, and then join line back with ,
- this will get you back a line of CSV data that you can push onto a result
array.
Finally at the end, join the result
array with \n
and there you have it - a new string containing CSV data without column you don't want.
Note that this will not properly handle quotes / other gotcha's that e with CSV data, but if your data doesn't contain any of that you're in the clear.
Sample using map:
// raw CSV data from somewhere
var csv =
'Category,Total,Under $1000\n' +
'Music,14744,1434,3450\n' +
'Art,12796,1216,7748\n';
// split on newlines, map over each line
var newCsv = csv.split('\n').map(function(line) {
var columns = line.split(','); // get the columns
columns.splice(1, 1); // remove total column
return columns;
}).join('\n'); // join on newlines
console.log(newCsv);
http://jsfiddle/PZ9vM/
Add the column removal as suggested by Trevor to this and your csv parser can handle stuff like quotes as well.
Maybe just use some splicing and slicing?
var data; //note, you're loading it in as a flat file with $.get()
$.get("/url/to/the/csvfile", function(d){
data = d.splice("\n");
var length = data.length; //so as to save loop runtime
for (var i = 0; i < length; i ++){
data[i] = data[i].splice(",")[0] + data[i].splice(",").slice(2)
//this will split data[i] into the first piece before the ma
//along with everything from the 2nd index to the end of the
//array that was spliced with ","
}
}
splicing and slicing are so much fun...
edit:
I forgot that you don't need to specify the end index with .slice()
.
Here's a nifty little vanilla JavaScript for you to remove a column from CSV data:
Working Demo
Code:
var csv =
'Category,Total,Under $1000\n' +
'Music,14744,1434,3450\n' +
'Art,12796,1216,7748\n';
// Made it a function to make it reusable!
function removeColumn(data, colIndex) {
var temp = data.split("\n");
for(var i = 0; i < temp.length; ++i) {
temp[i] = temp[i].split(",");
temp[i].splice(colIndex,1);
temp[i] = temp[i].join(","); // ment this if you want a 2D array
}
return temp.join("\n"); // returns CSV
return temp; // returns 2D array
return d3.csv.parse(temp); // returns a parsed object
}
console.log(removeColumn(csv,1));
本文标签: arraysParsing csv with javascript or d3js how to skip a columnStack Overflow
版权声明:本文标题:arrays - Parsing csv with javascript or d3js: how to skip a column? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745300357a2652334.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论