从 csv 中选择列(FMX、C++)

Pick columns from csv (FMX, C++)

我正在从一个大的 CSV 文件中读取一列数据(第 3 列)。我想知道是否有更有效的方法来做到这一点。现在我阅读每一行,然后选择我的专栏。

String S;
TStringList *List = new TStringList;
List->LoadFromFile(myCSV); // load the CSV file
std::auto_ptr<TStringList>pListA(new TStringList);
for (int i = 0; i < 400; i++) {   // for testing just getting first 400 rows.  
pListA->Text = StringReplace(List[0].Strings[i], ",", "\r\n", TReplaceFlags() << rfReplaceAll); 
S = pListA->Strings[3];  
// do something with this value from column 3
}

这段代码工作正常,但它必须读取整行 CSV 数据才能让我得到那一列。以下是 CSV 数据前两行的示例:

1,0,-6856,12830,8458,-16666,98717,-10718,-80874,49999,-99998,98569,99998,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

2,8948,-6856,12830,8458,-16666,39641,63125,-99973,99998,-99998,39385,99998,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

更新:根据 Remy 的代码,我在下面得到了这个。

    TStreamReader* Reader;
    Reader = new TStreamReader(myCSV);
    while (!Reader->EndOfStream)
     {
     String S = Reader->ReadLine();
     int start = PosEx(_D(","), S, 1);
     start = PosEx(_D(","), S, start+1) + 1;
     int end = PosEx(_D(","), S, start);
     S = S.SubString1(start, end-start);
             // use S 
     }
    Reader->Close();
    delete Reader->BaseStream;
    delete Reader;

根本不使用 TStringList,您可以使用 TStreamReader 来读取每一行,然后使用 Strutils::PosEx()String::SubString1() 简单地解析每一行以仅提取第 3 列值。

// NOTE: in C++11 and later, use std::unique_ptr instead!
std::auto_ptr<TStreamReader> Reader(new TStreamReader(myCSV)); // load the CSV file
while (!Reader->EndOfStream)
{
    String S = Reader->ReadLine();
    int start = PosEx(_D(","), S, 1);
    start = PosEx(_D(","), S, start+1) + 1;
    int end = PosEx(_D(","), S, start);
    S = S.SubString1(start, end-start);
    // do something with this value from column 3
}