Hello,
After importing a csv with
~csvFile = CSVFileReader.read(~dataPath ++ "/zku_noTime.csv", true, true).postcs;
I wanted to collect all values into a dictionary with the headers as keys and corresponding values in Arrays. So I can iterate through columns of corresponding headers. the csv file could only be accessed row by row in my understanding.
I came up with this solution,
(
~data = ();
var headers = List.new; // prepare lists for later data
~csvFile.do({
|line, i|
if (i==0, //headers
{ line.do({
|k, idx|
var header = k.toLower.asSymbol;
//"the key at index % is %".format(idx, header).postln;
headers.add(header);
~data.add(header -> List.new); // put headers in order of this iteration
})
},
{// rows with values
line.do({
|v, idx|
//"the value in data row at key % is %".format(headers[idx], v).postln;
~data[headers[idx]].add(v.asFloat); //add data element to corresponding key
})
};
);
});
)
I can’t stop wondering if there isn’t a simpler way to do this. like ~csvFile.collectColumns.
Is there?
thank you