www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to skip empty field in csvReader?

reply mw <mingwu gmail.com> writes:
Hi,

https://run.dlang.io/is/9afmT1

```
void main()
{
     import std.csv;
     import std.stdio: write, writeln, writef, writefln;
     import std.algorithm.comparison : equal;
     string text = "Hello;65;;\nWorld;123;7.5";
     struct Layout
     {
         string name;
         int value;
         double other;
     }

     auto records = text.csvReader!Layout(';');
     assert(records.equal([
         Layout("Hello", 65, 2.5),
         Layout("World", 123, 7.5),
     ]));


}
```

There is an empty field in the 1st line: "Hello;65;;", then

std.csv.CSVException /dlang/dmd/linux/bin64/../../src/phobos/std/csv.d(1232):
Floating point conversion error for input "".

Is there a way to tell csvReader to skip such empty fields?

Or, is there another CSV reader library with this functionality I 
can use?


Thanks.
Jun 05 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/6/23 1:09 AM, mw wrote:

 Is there a way to tell csvReader to skip such empty fields?
What I have done is specify that it's a string, and then handle the conversion myself. Possibly it can use Nullable, but I'm not sure.
 Or, is there another CSV reader library with this functionality I can use?
I don't know how much of this is supported in tsv-utils but you might give it a look. -Steve
Jun 06 2023
parent mw <mingwu gmail.com> writes:
On Tuesday, 6 June 2023 at 14:18:25 UTC, Steven Schveighoffer 
wrote:
 On 6/6/23 1:09 AM, mw wrote:

 Is there a way to tell csvReader to skip such empty fields?
What I have done is specify that it's a string, and then handle the conversion myself.
The std library need to be enhanced to skip such empty field (very simple change I think), it's a common scenario in real world data, which Python can handle easily.
 Possibly it can use Nullable, but I'm not sure.

 Or, is there another CSV reader library with this 
 functionality I can use?
I don't know how much of this is supported in tsv-utils but you might give it a look.
https://github.com/eBay/tsv-utils Do you know if there is any API doc? Readme only has command line doc.
Jun 06 2023