www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading a csv file

reply Andrew <nobody nowhere.invalid> writes:
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. 

This doesn't work: 

auto f = File(filename);
auto records = csvReader!int(f);

At this point it fails to compile with:

Error: template std.csv.csvReader does not match any function template 
declaration
Error: template std.csv.csvReader cannot deduce template function from 
argument types !(int)(File)
Error: template instance csvReader!(int) errors instantiating template

I assume this translates to "a file is not an acceptable form of input." 
Well, I know a string is, because the docs are written that way. But I'm 
not sure how to get a file into one big string, either; or if that's even 
the correct way to do it. 

Suggestions?

-- 
Andrew
Aug 09 2012
parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
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.
Aug 09 2012
parent reply Andrew <nobody nowhere.invalid> writes:
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
Aug 11 2012
parent reply "Nathan M. Swan" <nathanmswan gmail.com> writes:
On Saturday, 11 August 2012 at 14:45:28 UTC, Andrew wrote:
 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.
Without the -property switch, you can use non- property functions as if they were property. This is supposed to eventually be deprecated, so I try to not do this.
Aug 13 2012
parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
On Monday, 13 August 2012 at 18:49:55 UTC, Nathan M. Swan wrote:
 Without the -property switch, you can use non- property 
 functions as if they were  property. This is supposed to 
 eventually be deprecated, so I try to not do this.
To add to this, historically it was a feature. Why add properties when you can just call functions without parentheses? Long story left elsewhere, wiki4d seems to be down!!! sorry. Anyway, as it is a breaking change it takes forever to get rid of it (that way new code can start relying on it...)
Aug 13 2012
parent reply Andrew <nobody nowhere.invalid> writes:
On Tue, 14 Aug 2012 06:27:19 +0200, Jesse Phillips wrote:

 On Monday, 13 August 2012 at 18:49:55 UTC, Nathan M. Swan wrote:
 Without the -property switch, you can use non- property functions as if
 they were  property. This is supposed to eventually be deprecated, so I
 try to not do this.
To add to this, historically it was a feature. Why add properties when you can just call functions without parentheses? Long story left elsewhere, wiki4d seems to be down!!! sorry. Anyway, as it is a breaking change it takes forever to get rid of it (that way new code can start relying on it...)
Ah, understood. My thanks. I'll probably start using the -property switch just to avoid accidentally getting in the habit of using a deprecated feature. -- Andrew
Aug 15 2012
parent "Jesse Phillips" <Jessekphillips+D gmail.com> writes:
On Wednesday, 15 August 2012 at 15:13:09 UTC, Andrew wrote:

 Ah, understood. My thanks. I'll probably start using the 
 -property switch
 just to avoid accidentally getting in the habit of using a 
 deprecated
 feature.
Note, another form discussion has pointed out that -property is horribly broken and may not catch all and possibly denies what is valid. As I've never seen a official definition of what behavior can be expected, good luck.
Aug 15 2012