www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Row mismatch in CSV

reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
I'm adding output to the std.csv module that will be available in 
the next release and I'm hoping I can get an opinion on this.

The last stated rule for CSV is:

Each record should contain the same number of fields (not 
enforced)

I didn't enforce this as I don't see it likely to be indicating 
an error. But I have found that not following this and parsing 
with a struct will cause incorrect results.

one,two
three
four

Will parse as

one,two
three,two
four,two

So I can easily fix this by setting my internal struct with 
.init. I like this.

A goal I am going for with csvWriter is, what goes in comes out. 
But in this case your output would be

one,two
three,
four,

Which I like as it is correct, but it isn't the original input. 
So the only why to achieve these goals together is to not allow 
the initial invalid input... Thoughts?
Jan 09 2012
next sibling parent dennis luehring <dl.soluz gmx.net> writes:
Am 10.01.2012 06:08, schrieb Jesse Phillips:
 I'm adding output to the std.csv module that will be available in
 the next release and I'm hoping I can get an opinion on this.

 The last stated rule for CSV is:

 Each record should contain the same number of fields (not
 enforced)

 I didn't enforce this as I don't see it likely to be indicating
 an error. But I have found that not following this and parsing
 with a struct will cause incorrect results.

 one,two
 three
 four

 Will parse as

 one,two
 three,two
 four,two

 So I can easily fix this by setting my internal struct with
 .init. I like this.

 A goal I am going for with csvWriter is, what goes in comes out.
 But in this case your output would be

 one,two
 three,
 four,

 Which I like as it is correct, but it isn't the original input.
 So the only why to achieve these goals together is to not allow
 the initial invalid input... Thoughts?

do not allow invalid input - maybe a helper function to detect, but no silent "repair" or internal "allow-invalid" stuff - i would get harder to get the lib right
Jan 09 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Maybe we could optionally pass a func/delegate to std.csv which gets
invoked on these mismatches and figures out what to do? If no delegate
was passed then std.csv would just throw by default?
Jan 10 2012
prev sibling parent "Jesse Phillips" <jessekphillips+D gmail.com> writes:
On Tuesday, 10 January 2012 at 15:05:22 UTC, Andrej Mitrovic 
wrote:
 Maybe we could optionally pass a func/delegate to std.csv which 
 gets
 invoked on these mismatches and figures out what to do? If no 
 delegate
 was passed then std.csv would just throw by default?

That isn't going to happen. If for no other reason than it is limited in scope. I think the best course for someone parsing invalid CSV is to either turn off exceptions, or write their own parser with csvNextToken.
Jan 10 2012