2 summary:: file reader for comma separated data
7 CSVFileReader reads comma-separated text files into 2D arrays line by line.
9 For tab delimited files use link::Classes/TabFileReader::. For semi-colon-delimited files use link::Classes/SemiColonFileReader::. For space-delimited files, or custom delimiters, use link::Classes/FileReader::.
16 f = File("CSVReadTest.sc", "w");
18 "Some,comma,delimited,items, in line 1
20 and then, some more, with several commas,,,, in line 3
27 // open file, read and put strings into array, close file.
28 x = CSVFileReader.read("CSVReadTest.sc").postcs;
30 // can skip empty lines:
31 x = CSVFileReader.read("CSVReadTest.sc", true).postcs;
33 // can skip blank entries caused by multiple commas:
34 x = CSVFileReader.read("CSVReadTest.sc", true, true).postcs;
36 // do file open/close by hand if you prefer:
37 f = File("CSVReadTest.sc", "r"); f.isOpen;
39 t.read(true, true).postcs;
43 // write a test file with numbers:
44 f = File("CSVReadTestNum.sc", "w");
46 (1..10).do { |n| f.write(n.asString ++ ","); };
50 x = CSVFileReader.read("CSVReadTestNum.sc", true, true).postcs;
51 x.collect(_.collect(_.interpret)); // convert to numbers.
53 // or do it immediately:
54 x = CSVFileReader.readInterpret("CSVReadTestNum.sc").postcs;
57 // write a test file with several lines of numbers:
58 f = File("CSVReadTestNum.sc", "w");
61 f.write(n.asString ++ if (n % 10 != 0, ",", Char.nl)); };
66 x = CSVFileReader.readInterpret("CSVReadTestNum.sc", true, true).postln;