首页 > 解决方案 > 如何将数据作为额外列写入 .csv 文件

问题描述

我有一个这样的csv文件

NAME, BANK ACCOUNT, SORT CODE, INVESTMENT
 A     123            XXX         1000
 B     456            XXX         2000
 C     789            XXX         3000

我想添加一个额外的列,使文件看起来像

NAME, BANK ACCOUNT, SORT CODE, INVESTMENT, Contribution
 A     123            XXX         1000         16.67
 B     456            XXX         2000         33.33
 C     789            XXX         3000          50

我已经制定了一个float percentage包含所有必需元素的方法,现在我对如何将它作为额外的行添加到数据文件中感到困惑。

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>

using namespace std;

#define W1 0.2

vector<float>Data_investment;
string Trim(string& str);

string Trim(string& str)
{

    str.erase(0, str.find_first_not_of(" \t\r\n"));
    str.erase(str.find_last_not_of(" \t\r\n") + 1);
    return str;
}

void Get_Data(const string& s, vector<float>& data)
{
    ifstream fin(s);
    string line;
    int flag = 0;
    while (getline(fin, line))
    {
        if (flag)
        {
            istringstream sin(line);
            vector<string> fields;
            string field;
            while (getline(sin, field, ',')) 
            {
                fields.push_back(field);  
            }
            string investment = Trim(fields[3]);
            data.push_back(atof(const_cast<const char*>(investment.c_str())));
        }
        ++flag;
    }
}

int main()
{
    Get_Data("aaa.csv", Data_investment);

    int sum = 0;
    for (auto i : Data_investment) {
        sum += i;
    }

    float total = accumulate(Data_investment.begin(), Data_investment.end(), 0);
    const unsigned int quantity = Data_investment.size();
    for (unsigned int i = 0; i < quantity; ++i)
    {
        float percentage = (Data_investment[i] * 100.0f) / total;
        cout << percentage << ", ";
    }
    cout << endl;

    return 0;
}

标签: c++csvvector

解决方案


尝试这样的事情:

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>

using namespace std;

struct Investment
{
    string Name;
    string BankAccount;
    string SortCode;
    float Investment;
    float Contribution;
};

string Trim(string str)
{
    str.erase(0, str.find_first_not_of(" \t\r\n"));
    str.erase(str.find_last_not_of(" \t\r\n") + 1);
    return str;
}

void Get_Data(const string &filename, vector<Investment> &data)
{
    ifstream fin(filename);
    if (!fin.is_open())
        return;

    string line;
    if (!getline(fin, line))
        return;

    while (getline(fin, line))
    {
        istringstream sin(line);
        vector<string> fields;
        string field;

        while (getline(sin, field, ','))
        {
            fields.push_back(field);  
        }

        Investment inv;
        inv.Name = Trim(fields[0]);
        inv.BankAccount = Trim(fields[1]);
        inv.SortCode = Trim(fields[2]);
        inv.Investment = atof(Trim(fields[3]).c_str());
        inv.Contribution = 0.0f;

        data.push_back(inv);
    }
}

void Save_Data(const string &filename, const vector<Investment> &data)
{
    ofstream fout(filename);
    if (!fout.is_open())
        return;

    fout << "NAME,BANK ACCOUNT,SORT CODE,INVESTMENT,Contribution\n";

    for (auto &inv : data)
    {
        fout << inv.Name << ","
            << inv.BankAccount << ","
            << inv.SortCode << ","
            << inv.Investment << ","
            << inv.Contribution << "\n";
    }
}

int main()
{
    vector<Investment> Data_investment;
    Get_Data("aaa.csv", Data_investment);

    float total = accumulate(Data_investment.begin(), Data_investment.end(), 0.0f,
        [](float sum, const Investment &inv){ return sum + inv.Investment; }
    );

    for (auto &inv : Data_investment)
    {
        inv.Contribution = (inv.Investment * 100.0f) / total;
    }

    Save_Data("aaa_new.csv", Data_investment);

    return 0;
}

现场演示


推荐阅读