简体   繁体   English

C++ - 将 .csv 文件读入向量,同时跳过第一行和特定列

[英]C++ - Reading .csv file into vectors, whilst skipping first line and specific columns

I'm trying to read in a .csv file that looks like this:我正在尝试读取如下所示的 .csv 文件:

Name,Place,Age,x,y
A,X,1,50,100
B,Y,2,-90,20
C,Z,3,0.4,80
...

Except there are 100 rows of data (plus the header).除了有 100 行数据(加上标题)。

I would like to read in the columns Name, Age, x and y and place them in an vector that looks like this:我想读入 Name、Age、x 和 y 列,并将它们放入如下所示的向量中:

Name = [Age, x, y]

and do this for all 100 rows (so 100 vectors).并对所有 100 行(即 100 个向量)执行此操作。

I've tried searching the forum for help but the best help I could get was from c++ Skip first line of csv file , which I modified slightly just to print the Age, x, y.我试过在论坛上搜索帮助,但我能得到的最好帮助来自c++ Skip first line of csv file ,我稍微修改了一下,只是为了打印年龄,x,y。

ifstream data("data.csv");
    if (!data.is_open())
    {
        exit(EXIT_FAILURE);
    }
    string str;
    getline(data, str); // skip the first line
    while (getline(data, str))
    {
        istringstream iss(str);
        string token;
        while (getline(iss, token, ','))
        {
            double Age_x_y = atof(token.c_str());

            if (Age_x_y != 0) {
                cout << Age_x_y << " ";
            }
            cout << endl;
        }
    }

This is nice if it was all I wanted to output, but I believe all the data is just stored as a double.如果这就是我想要输出的全部内容,那就太好了,但我相信所有数据都存储为双精度值。 I need the data stored in a vector (or something else like a structure) so that I can manipulate the entries.我需要存储在向量(或其他类似结构的东西)中的数据,以便我可以操作条目。 For example, I would like to work out x+y for each Name.例如,我想为每个名称计算 x+y。

How can I extract the data in this way?如何以这种方式提取数据?

Any help would be greatly appreciated.任何帮助将不胜感激。

Your parsing of the CSV is good, but be careful that you don't provide any data where the fields have embedded commas.您对 CSV 的解析很好,但请注意不要在字段嵌入逗号的地方提供任何数据。 Some CSV formatters allow a field to contain a comma if the field is enclosed in double-quotes for example.例如,如果字段用双引号括起来,一些 CSV 格式器允许字段包含逗号。

You can create a structure which represents (more or less) a single line in your CSV file.您可以创建一个结构来表示(或多或少)CSV 文件中的一行。

struct Record
{
    std::string name;
    std::string place;
    int age;
    double x;
    double y;
};

Now, you can parse each line into a Record object (pick an appropriate name for the struct), and then place each Record into a vector.现在,您可以将每一行解析为一个Record对象(为结构选择一个合适的名称),然后将每个Record放入一个向量中。 Make sure to include <string> and <vector> .确保包含<string><vector>

std::vector<Record> my_records;
while (getline(data, str))
{
    Record record;
    istringstream iss(str);
    string token;

    getline(iss, record.name, ',');
    getline(iss, record.place, ',');

    // use atoi(token.c_str()) if you don't have std::stoi from C++11
    getline(iss, token, ',');
    record.age = std::stoi(token);

    // use atof(token.c_str()) if you don't have std::stod from C++11
    getline(iss, token, ',');
    record.x = std::stod(token);

    getline(iss, token, ',');
    record.y = std::stod(token);

    my_records.push_back(record);
}

// loop through the vector, summing every possible
// combination of x values. The outer loop goes from
// index 0 to the second-to-last index, while the inner
// loop goes from the current outer loop counter to the
// last index.

// Note that this is O(n^2) complexity (even with a
// diminishing inner loop). Increasing the number of
// records can have a very noticeable effect on running
// time.
for (size_t i = 0; i < my_records.size() - 1; i++)
{
    for (size_t j = i + 1; j < m_records.size(); j++)
    {
        std::cout << my_records[i].name << ".x + ";
        std::cout << my_records[j].name << ".x = ";
        std::cout << (my_records[i].x + my_records[j].x) << std::endl;
    }
}

You can also use my_records[i].x + my_records[i].y if you want to output the sum of those values.如果您想输出这些值的总和,您也可以使用my_records[i].x + my_records[i].y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM