CSV with empty values for integer fields
Arun Chandrasekaran
aruncxy at gmail.com
Sat Oct 28 21:58:39 UTC 2017
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
More information about the Digitalmars-d-learn
mailing list