Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the csv-parser to get all the values of a specific column #10

Closed
CleanHit opened this issue Jul 12, 2018 · 4 comments
Closed

Using the csv-parser to get all the values of a specific column #10

CleanHit opened this issue Jul 12, 2018 · 4 comments

Comments

@CleanHit
Copy link

Is there a way to iterate over the values of a specific column using a column name (string)? I've searched the documentation but didn't find a clear answer or an example to my question.

@vincentlaucsb
Copy link
Owner

Here's an example for printing the values of a specific column. I agree the documentation is a work in progress.

CSVReader reader("my_file.csv");

for (auto& row: reader) {
    std::cout << row["column_name"].get<>() << std::endl;
}

The documentation for the CSVRow object (auto& row) here (https://vincentlaucsb.github.io/csv-parser/classcsv_1_1CSVRow.html) describes the indexing and value retrieval methods.

@CleanHit
Copy link
Author

CleanHit commented Jul 12, 2018

Thanks for the fast reply, I actually saw that yesterday in the documentation and did try it like you wrote, but my IDE gave me some syntax error. Today it worked without any errors, maybe I just needed to restart my IDE....

This brings me to another question. Is this there a more package native way to iterate over each item of some column and being able to change their values? Rather than using this:

vector<double_t> pickupLongitude;
for (csv::CSVRow &row: reader) {
    
    pickupLongitude.push_back(row["Pickup_longitude"].get<double_t>());
}

for (auto &item : pickupLongitude){
    
    \\ Do something with item
}

If I understood it correctly I can iterate over the items with csv::SCVField, but I can't change their value with it. Am I wrong?

@vincentlaucsb
Copy link
Owner

vincentlaucsb commented Jul 12, 2018

The CSVReader was designed/optimized for reading values. If you want to modify the values, I would just have some custom logic populate another std::vector like you are doing right now.

However it is still possible to copy a CSVRow to a std::vector<std::string> by typing

std::vector<std::string>(row)

which you might find helpful.

The reason why you can't modify values is because CSVFields are just thin wrappers over string_views. Of course I could implement some feature to modify the original string, but that'd be very inefficient. It's probably a lot cheaper to just copy what you need and modify that. Previous versions of the CSVReader spit out rows as std::vector<std::string>, but those implementations were 30% slower.

@CleanHit
Copy link
Author

Thanks and good to know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants