【问题标题】:How to get a specific column from a csv file?如何从 csv 文件中获取特定列?
【发布时间】:2022-12-05 03:11:08
【问题描述】:

在这里,我尝试从 csv 文件中获取特定列,但在 ptr[rowIdx].push_back(value) 语句中出现错误。它给出了一个关于“值”的错误,它不是从字符串到字符的合适转换。

这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm> 
#include<string.h>
#include<cstdlib>
//std::find
#include<cstring>
using namespace std;
int main(int argc, char** argv)
{
    ifstream fin("filename");
    string line;
    int rowCount = 0;
    int rowIdx = 0; //keep track of inserted rows

    //count the total nb of lines in your file
    while (getline(fin, line)) {
        rowCount++;
    }

    //this will be your table. A row is represented by data[row_number].
    //If you want to access the name of the column #47, you would
    //cout << data[0][46]. 0 being the first row(assuming headers)
    //and 46 is the 47 column.
    //But first you have to input the data. See below.
    string *ptr=new string[rowCount];

    fin.clear(); //remove failbit (ie: continue using fin.)
    fin.seekg(fin.beg); //rewind stream to start

    while (getline(fin, line)) //for every line in input file
    {
        stringstream ss(line);  //copy line to stringstream
       string value;
        while (getline(ss, value, ',')) {       //for every value in that stream (ie: every cell on that row)
            ptr[rowIdx].push_back(value);//add that value at the end of the current row in our table
        }
        rowIdx++;   //increment row number before reading in next line
    }
fin.close();


//Now you can choose to access the data however you like.
//If you want to printout only column 47...

int colNum = 1;  //set this number to the column you want to printout

for (int row = 0; row < rowCount; row++)
{
    cout << ptr[row][colNum] << "\t";  //print every value in column 47 only
}
cout << endl;


return 0;
}

请告诉我问题出在哪里。

【问题讨论】:

  • std::stringpush_back()成员参加了单个字符作为它的论据。请改用append 成员或仅使用ptr[rowIdx] += value;。如果没有重复我会感到惊讶,但这个是相关的:stackoverflow.com/q/15082170/10871073

标签: c++


【解决方案1】:

使用字符串矩阵读取 CSV 文件会更好:

  • 无需解析文件两次。
  • 无需统计文件中的行数。
  • 您避免newing 内存。

[Demo]

#include <cassert>
#include <iostream>  // cout
#include <sstream>  // istringstream
#include <string>
#include <vector>

int main() {
    std::istringstream file_iss{R"(a,b,c,d,e
1,2,3,4,5
!,@,#,¿,*
)"};

    using row_t = std::vector<std::string>;
    using file_t = std::vector<row_t>;
    file_t file{};

    std::string line{};
    while (getline(file_iss, line)) {
        row_t row{};
        std::istringstream line_iss{ line };
        std::string value{};
        while (getline(line_iss, value, ',')) {
            row.push_back(std::move(value));
        }
        file.push_back(std::move(row));
    }

    size_t col{ 1 };
    assert((not file.empty()) and (col < file[0].size()));
    for (size_t row{ 0 }; row < file.size(); row++) {
        std::cout << file[row][col] << "	";
    }
    std::cout << "
";
}

// Outputs: b   2   @

  • 通知 string* ptr = new string[rowCount]; 不正确的原因有很多:
  • 您正在创建一个字符串数组。这将用于存储每一行​​。但是,如果您还想存储每一列,则需要一组字符串数组,例如 string** ptr = new string*[rowCount];
  • 然后,一旦您读取了一行,您应该对其进行解析以了解列数,并为该给定行创建一个字符串数组:file[rowIdx] = new std::string[colCount];
  • 最后,您还需要第二个计数器,例如colIdx,保存每一列的值:file[rowIdx][colIdx] = value;

[Demo]

#include <cassert>
#include <iostream>  // cout
#include <sstream>  // istringstream
#include <string>

int main() {
    std::istringstream file_iss{R"(a,b,c,d,e
1,2,3,4,5
!,@,#,¿,*
)"};

    std::string** file = new std::string*[3];
    std::string line{};
    int rowIdx{ 0 };
    while (std::getline(file_iss, line)) {
        std::istringstream line_iss(line);
        std::string value{};
        file[rowIdx] = new std::string[5];
        int colIdx{ 0 };
        while (std::getline(line_iss, value, ',')) {
            file[rowIdx][colIdx] = value;
            ++colIdx;
        }
        ++rowIdx;
    }
    size_t col{ 1 };
    for (size_t row{ 0 }; row < 3; ++row) {
        std::cout << file[row][col] << "	";
    }
    std::cout << "
";
}

【讨论】:

    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多