如何读取CSV文件并分配给Eigen Matrix?

D_9*_*268 2 c++ csv matrix eigen

我尝试将一个大的cvs文件读入Eigen Matrix,在发现有问题的代码下面,它无法检测cvs文件中的每行\n在矩阵中创建多行.(它用单行读取整个文件).不确定代码有什么问题.谁能在这里建议?我也在寻找一种有效的方法来读取10k行和1k cols的csv文件.不太确定下面的代码是最有效的方法吗?非常感谢你的评论.

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <istream> //DataFile.fail()  function
#include <vector>
#include <set>
#include <string>
using namespace std;


#include <Eigen/Core>
#include <Eigen/Dense>
using namespace Eigen;

 void readCSV(istream &input, vector< vector<string> > &output)
{
    int a = 0;
    int b = 0;

    string csvLine;
    // read every line from the stream
    while( std::getline(input, csvLine) )
    {

        istringstream csvStream(csvLine);
        vector<string> csvColumn;
        MatrixXd mv;
        string csvElement;
        // read every element from the line that is seperated by commas
        // and put it into the vector or strings
        while( getline(csvStream, csvElement, ' ') )
        {
            csvColumn.push_back(csvElement);
            //mv.push_back(csvElement);
            b++;
        }       
        output.push_back(csvColumn);
        a++;
    }
    cout << "a : " << a << " b : " << b << endl;   //a doen't detect '\n'
}

int main(int argc, char* argv[])
{

    cout<< "ELM" << endl;
    //Testing to load dataset from file.
    fstream file("Sample3.csv", ios::in);
    if(!file.is_open())
    {
        cout << "File not found!\n";
        return 1;
    }
    MatrixXd m(3,1000);
    // typedef to save typing for the following object
    typedef vector< vector<string> > csvVector;
    csvVector csvData;

    readCSV(file, csvData);
    // print out read data to prove reading worked
    for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
    {
        for(vector<string>::iterator j = i->begin(); j != i->end(); ++j)
        {
           m(i,j) = *j; 
           cout << *j << ", ";
        }
        cout << "\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

我还将附上一个示例cvs文件.https://onedrive.live.com/redir?resid=F1507EBE7BF1C5B!117&authkey=!AMzCnpBqxUyF1BA&ithint=file%2ccsv

use*_*269 14

这是你可以实际复制粘贴的东西

编写自己的"解析器"

优点:轻巧且可定制

缺点:可定制

#include <Eigen/Dense>
#include <vector>
#include <fstream>

using namespace Eigen;

template<typename M>
M load_csv (const std::string & path) {
    std::ifstream indata;
    indata.open(path);
    std::string line;
    std::vector<double> values;
    uint rows = 0;
    while (std::getline(indata, line)) {
        std::stringstream lineStream(line);
        std::string cell;
        while (std::getline(lineStream, cell, ',')) {
            values.push_back(std::stod(cell));
        }
        ++rows;
    }
    return Map<const Matrix<typename M::Scalar, M::RowsAtCompileTime, M::ColsAtCompileTime, RowMajor>>(values.data(), rows, values.size()/rows);
}
Run Code Online (Sandbox Code Playgroud)

用法:

MatrixXd A = load_csv<MatrixXd>("C:/Users/.../A.csv");
Matrix3d B = load_csv<Matrix3d>("C:/Users/.../B.csv");
VectorXd v = load_csv<VectorXd>("C:/Users/.../v.csv");
Run Code Online (Sandbox Code Playgroud)

使用armadillo库的解析器

优点:支持其他格式,而不仅仅是csv

缺点:额外的依赖

#include <armadillo>

template <typename M>
M load_csv_arma (const std::string & path) {
    arma::mat X;
    X.load(path, arma::csv_ascii);
    return Eigen::Map<const M>(X.memptr(), X.n_rows, X.n_cols);
}
Run Code Online (Sandbox Code Playgroud)


Luc*_*dyt -4

这将正确读取 csv 文件:

std::ifstream indata;

indata.open(filename);

std::string                line;
while (getline(indata, line))
{
    std::stringstream          lineStream(line);
    std::string                cell;

    while (std::getline(lineStream, cell, ','))
    {
        //Process cell
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:此外,由于您的 csv 充满了数字,std::stod一旦您希望将它们视为这样,请确保使用或等效的转换。