ben-strasser / fast-cpp-csv-parser

fast-cpp-csv-parser
BSD 3-Clause "New" or "Revised" License
2.11k stars 440 forks source link

Seems cumbersome interface in certain cases? #55

Open kb1ooo opened 6 years ago

kb1ooo commented 6 years ago

Say I have a CSV file with 30 columns, an unreliable header (but order is guaranteed), and I only care about 4 columns. So, I've got to call set_header with 30 names for the columns. Then in my read call I need to pass 30 variables. Is that correct? Seems cumbersome, even if I needed all 30 columns. It would be nice to have an option where the row elements are returned in std::vector and then I do the data type conversions myself for the columns I need.

ben-strasser commented 6 years ago

Hi,

if you want a vector, use LineReader and split the string at the separator.

I think what you really want is something like:

CSVReader::set_partial_header(ignore_column ignore_policy, string_type col_name1, int col_pos1, string_type col_name2, int col_pos2 ...)

This should not be too hard to implement. What you need to do is fill the following CSAReader members: col_order, column_names, ignore_policy. I think the function would essentially need to do:

this->ignore_policy = ignore_policy; this->col_order = {col_pos1, col_pos2, ...}; this->col_name = {col_name1, col_name2, ...};

Tested pull requests are welcome.

Best Regards Ben Strasser

mom1705 commented 4 years ago

Is it possible that we can set header but not make it compulsory. someone might choose not to fill, currently it is crashing if we set it but don't fill in csv file