ben-strasser/fast-cpp-csv-parser

default value with set_header

Closed this issue · 8 comments

Hi,

I have a 3 column header-less CSV that looks like this:

# comment
a,b,c
c,a,b

1,2,3
red,orange,blue
tall,medium
apple,orange,tomatoes

I am attempting to read it with:

io::CSVReader<3, io::trim_chars<' '>, io::double_quote_escape<',', '\"'>, io::single_and_empty_line_comment<'#'>> csv_reader(filename);

csv_reader.set_header("Col A", "Col B", "Col C");

string col_a, col_b, col_c;

while (csv_reader.read_row(col_a, col_b, col_c)) {
  cout << col_a << " " << col_b << " " << col_c << "\n";
}

I get an exception thrown (io::error::too_few_columns) at the "tall,medium" line as it is missing a column. Is there a a way to add a default value (such as a simple null or "") for any missing column like it is possible with read_header? I saw this reported issue, but there wasn't really a solution other than adding a header in the file (I have no control on the file).

An example would be appreciated!

Hi,

The way I see it, in the case of "tall,medium", the 3rd column is missing because the separator is there. I have it like that in my head:

tall,,medium #missing second column
,tall,medium #missing first column
tall,medium #missing third column

Which would translate to:
tall,NULL,medium
NULL,tall, medium
tall,medium,NULL

Even if you add a header, it won't make the missing data suddenly appear. To me, having the header in the file or hardcoded in the source code seems the same.

EDIT: I understand your thought process, but I just wished that you could set a column as "optional", maybe it's always minimum 2 columns and the 3rd one could be filed or not.

Cheers!

An example of such file is this wireshark manuf file, if you CTRL-F for "00:00:13", you will see an example of the first 2 columns being present and the 3rd missing.

If you look at the link above and you search for "00:00:13", you will see an example where there are 2 columns available, the 3rd is missing. If you look in the entire file, there is always 2 columns, and about 95% of all rows have a 3rd column with data. I am trying to replace the 5% left with "NULL" and not have an exception thrown.

Regex it is, thanks.