[phobos] CSVRange: RFC
David Simcha
dsimcha at gmail.com
Sat Jan 29 15:44:11 PST 2011
I've written a small module for reading CSV and similar delimited
files. I've been meaning to do this for a while. Basically, it allows
reading a CSV file with O(1) memory usage (i.e. it can be parsed one
character at a time) to a range of ranges of cells. Quotes, escaped
quotes, etc. are handled properly. I tested it on a nasty CSV file
produced by Affymetrix, and it works rather well.
CSVRange also allows for iteration over rows as a range of structs. For
example, let's say you had a file:
Height,Weight,Shoe Size
6.5,210,13
...
You could read this file lazily into a range of structs with something like:
struct Person
{
float height;
uint weight;
uint shoeSize;
}
auto csvRange = csvFile(someCharacterRange, ',');
auto structs = csvStructRange(csvRange, ["Height", "Weight", "Shoe Size"]);
// Iterate lazily through the rows.
foreach(s; structs) {
// Do stuff.
}
Note that this still works even if you have tons of columns you don't
care about in the file.
Code:
http://dsource.org/projects/scrapple/browser/trunk/csvRange/csvRange.d
Docs:
http://cis.jhu.edu/~dsimcha/csvRange.html
More information about the phobos
mailing list