www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - CSV with empty values for integer fields

reply Arun Chandrasekaran <aruncxy gmail.com> writes:
CSV with empty values for integer fields throws

(Row: 1, Col: 3) Unexpected end of input when converting from 
type string to type int

Code that parses the CSV:

```
import std.algorithm;
import std.array;
import std.csv;
import std.stdio;
import std.conv;
import std.range;

private struct IdentifyResult
{
     string probe;
     string target;
     int rank;
     int score;
     bool vendorMatch;
}

void main(string[] args)
{
     if (args.length < 2)
     {
         writeln("Usage: ", args[0], " <csv-file-1> ... 
<csv-file-n>");
         return;
     }

     foreach (i; 1 .. args.length)
     {
         try
         {
             auto arr = File(args[i], 
"r").byLine.joiner("\n").csvReader!IdentifyResult.array;
             writeln(args[i], "\tValid");
         }
         catch (std.csv.CSVException e)
         {
             writeln(e);
             writeln(args[i], "\tInvalid");
         }
     }
}
```

Input CSV file below

CSV header (just for reference, but not part of the CSV)
Probe,Target,rank,score,match/nomatch,datetime,position,score
```
LIP_0905_1230.nist,,,,FALSE,2017-09-05 23:24:37,,
LIP_0905_1230.nist,,,,FALSE,2017-10-12 11:37:29,,
LIP_0905_1230.nist,,,,FALSE,2017-10-12 11:51:03,,
LIP_0905_1230.nist,,,,FALSE,2017-10-12 12:07:21,,
LIP_0905_1230.nist,CRM_1012_1920.nist,1,9999,true,2017-10-12 
19:56:00,25,9999
LIP_0905_1230.nist,CRM_1012_1920.nist,1,9999,true,2017-10-13 
00:55:00,25,9999
LIP_0905_1230.nist,CRM_1013_0005.nist,2,9999,true,2017-10-13 
00:55:00,25,9999
LIP_0905_1230.nist,CRM_1012_1920.nist,1,9999,true,2017-10-18 
18:27:22,25,9999
LIP_0905_1230.nist,CRM_1013_0005.nist,2,9999,true,2017-10-18 
18:27:22,25,9999
LIP_0905_1230.nist,CRM_1013_0005.nist,1,9999,true,2017-10-20 
11:04:31,25,9999
```


Is there anyway to overcome this without modifying the original 
CSV?


Cheers,
Arun
Oct 28 2017
parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
Not really you'll need to parse it out as a string and do the 
conversion later.

It probably would be good to support nullable!int pretty sure it 
doesn't currently.
Oct 29 2017
parent Arun Chandrasekaran <aruncxy gmail.com> writes:
On Sunday, 29 October 2017 at 15:45:23 UTC, Jesse Phillips wrote:
 Not really you'll need to parse it out as a string and do the 
 conversion later.

 It probably would be good to support nullable!int pretty sure 
 it doesn't currently.
Should I raise a ticket on Bugzilla to address this?
Oct 30 2017