c++阅读csv文件

qpgpyjmq  于 2023-10-13  发布在  其他
关注(0)|答案(4)|浏览(132)

我想通过使用C++读取CSV文件,所以这里是我的代码

int main(){
 ifstream classFile("class.csv");
 vector<string> classData;

 while (getline(classFile, line,',')) // there is input overload classfile
        {
            classData.push_back(line);  

        }
}

我的问题是:我的问题是,当它读取每行的最后一列时(因为它不是由逗号分隔的),它读取最后一列数据和下一行数据的第一个,例如,如果我的数据是这样的,
className,classLocation,Professor c++,Library,John
然后它读起来像className/ classLocation/ Professor c++/ Library / John
有没有办法把最后一列和下一行的第一列分开?谢谢你,很抱歉这是混乱的

j2datikz

j2datikz1#

逐行读取文件:

std::string line;
while(std::getline(stream, line)) ...

将每一行传递到istingstream并读取字段:

std::istringstream s(line);
std::string field;
while (getline(s, field,',')) ...

免责声明:这是对csv文件的简化解析。

pbpqsu0x

pbpqsu0x2#

对不起,我能在这条线里插点纯C吗?
阅读csv很清楚:

#include <stdio.h>

int main()
{
  float f1, f2;

  FILE *fp;
  fp = fopen("file.csv", "r");

  while (fscanf(fp, "%g,%g\n", &f1, &f2) == 2)
    printf("%g\n", f1+f2);
}

当然,它应该在C++工作的地方工作。
while中,我们检查fscanf找到了多少个对象:fscanf(fp, "%g,%g\n", &f1, &f2) == 2-- fscanf返回找到的对象数。
我希望这可能对某人有所帮助。
(And如果有人想看到更多关于fscanf和阅读文件的信息--请留下一些评论。)

ohtdti5x

ohtdti5x3#

如果您有兴趣在main部分中调用void函数,请查看以下代码:

void readCSV(const string &strPath2Dataset)
{   
    ifstream csvFile;
    string strPathCSVFile = strPath2Dataset + "/test.csv";
    csvFile.open(strPathCSVFile.c_str());

    if (!csvFile.is_open())
    {
        cout << "Path Wrong!!!!" << endl;
        exit(EXIT_FAILURE);
    }

    vector<long double> timeStampIMU;
    vector<long double> gyro_X;
    vector<long double> gyro_Y;
    vector<long double> gyro_Z;

    vector<long double> acc_X;
    vector<long double> acc_Y;
    vector<long double> acc_Z;

    string line;
    vector <string> vec;
    getline(csvFile, line); // skip the 1st line

    while (getline(csvFile,line))
    {
        if (line.empty()) // skip empty lines:
        {
            //cout << "empty line!" << endl;
            continue;
        }

        istringstream iss(line);
        string lineStream;
        string::size_type sz;

        vector <long double> row;

        while (getline(iss, lineStream, ','))
        {  
            row.push_back(stold(lineStream,&sz)); // convert to double
        }

        timeStampIMU.push_back(row[0]);

        gyro_X.push_back(row[1]);
        gyro_Y.push_back(row[2]);
        gyro_Z.push_back(row[3]);

        acc_X.push_back(row[4]);
        acc_Y.push_back(row[5]);
        acc_Z.push_back(row[6]);
    }

    //cout << "size ts = " << timeStampIMU.size() << endl;
    for (size_t i = 0; i < timeStampIMU.size(); i++)
    {
        cout << "ts_imu = " << setprecision(12) << timeStampIMU[i] << endl;

        cout << "gx = " << setprecision(12) << gyro_X[i] << endl;
        cout << "gy = " << setprecision(12) << gyro_Y[i] << endl;
        cout << "gz = " << setprecision(12) << gyro_Z[i] << endl;

        cout << "ax = " << setprecision(12) << acc_X[i] << endl;
        cout << "ay = " << setprecision(12) << acc_Y[i] << endl;
        cout << "az = " << setprecision(12) << acc_Z[i] << endl;
        cout << "--------------------------------" << endl;
    }
}

我的.csv文件是IMU传感器提供的数据集,用逗号分隔:

TimeStamp, Gyro_X, Gyro_Y, Gyro_Z, Acc_X, Acc_Y, Acc_Z
9vw9lbht

9vw9lbht4#

我发现的所有函数都不支持转义字符,也不考虑将,(如Unicode字符)以外的任何字符作为分隔符,尽管后者经常用于普通字符串。这是我的DSV-reader版本,它支持转义。它可以转义分隔符,转义字符和换行符。您可以通过检查列索引i手动转换类型。

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

std::vector<std::vector<std::wstring>> DSVToArray(std::wstring text) {
    const wchar_t ch_sep = L'→';
    const wchar_t ch_esc = L'¶';
    const wchar_t ch_newline = L'\n';
    wchar_t pletter = L'\0';
    std::vector<std::wstring> columns = {L""};
    std::vector<std::vector<std::wstring>> rows = {};
    int i = 0;
    int r = 0;
    bool unescaped = true;
    bool is_last = (0 >= text.size() - 1);
    for(std::string::size_type pos = 0; !is_last; ++pos) {
        wchar_t letter = text[pos];
        is_last = (pos >= text.size() - 1);
        if (ch_esc == letter) {
            if (!unescaped && pletter == ch_esc) {
                columns[i] += letter;
            }
            unescaped = !unescaped;
        }
        else if (ch_sep == letter && unescaped) {
            ++i;
            columns.push_back(L"");
            letter = L'\0';
        } else if (ch_newline == letter && unescaped || is_last) {
            if (L'\r' == pletter)
                columns[i] = columns[i].substr(0, columns[i].size() - 1);
            if (ch_newline != letter)
                columns[i] += letter;
            if (columns.size() == 1 && columns[0].size() == 0)
               break;
            rows.push_back({columns});
            columns = {L""};
            i = 0;
            letter = L'\0';
        }
        else {
            columns[i] += letter;
            if (!unescaped)
                unescaped = true;
        }
        pletter = letter;
    }
    return rows;
}

示例用法:

std::wstring join(const std::wstring& sep, const std::vector<std::wstring>& values)
{
    std::wstringstream result;
    for(int i = 0; i < values.size(); i++ )
        result << ( i ? sep : L"" ) << values[i];
    return result.str();
}

int main() {
    std::vector<std::vector<std::wstring>> result = DSVToArray(
        L"a¶→bc→ff¶¶ee\ncc→dd\nee→ff ff"
    );
    for (const auto& columns : result) {
        std::wcout << join(L" -> ", columns) << std::endl;
    }
    std::wcout << std::endl;
    return 0;
}

示例的输出:

a→bc -> ff¶ee
cc -> dd
ee -> ff ff

相关问题