Reading a csv file
Andrew
nobody at nowhere.invalid
Sat Aug 11 07:45:28 PDT 2012
On Fri, 10 Aug 2012 03:44:11 +0200, Jesse Phillips wrote:
> On Friday, 10 August 2012 at 01:39:32 UTC, Andrew wrote:
>> I'm trying to read in a csv file. The examples in the docs for std.csv
>> all assume you're reading from a string rather than a file.
>
> It requires a range of dchar. I believe there is an undocumented
> function to get a dchar range out of a File, but as it is
> undocumented...
>
> std.file.readText() will be your friend.
This works perfectly, and thank you. I've since run into something
peculiar. Here's my csv loader. It stuffs a csv file into an associative
array containing structs of an arbitrary type, so long as the struct
defines csvid(), which is needed to tell it what to use as the array key:
T[string] loadcsvtable(T)(string filename)
{
string input = readText(filename);
auto records = csvReader!T(input, null);
T[string] table;
foreach(record; records)
table[record.csvid] = record; <---NOTE record.csvid
return table;
}
Only I forgot the parenthesis on csvid()...and it still works. And I
can't figure out why. I know you can do this sort of thing with
@property, but the method definition doesn't have @property set.
I'm not precisely complaining, but something's going on here I don't
understand.
--
Andrew
More information about the Digitalmars-d-learn
mailing list