gpt4 book ai didi

c++ - 解析具有 CR LF EOL 结构的 .csv 文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:14 24 4
gpt4 key购买 nike

我正在尝试解析一个 CSV 文件,getline() 将整个文件作为一行读取。假设 getline() 没有得到预期的结果,我尝试了 \r\n\n\r\r\n\0 作为参数没有运气。

我查看了 EOL 字符和 CR,然后是 LFgetline() 只是忽略了这个还是我遗漏了什么?另外,这里的修复是什么?

此函数的目标是将数据存储为字符串的二维 vector 的通用 CSV 解析函数。尽管在这方面的建议是受欢迎的,但我只是在寻找解决此问题的方法。

vector<vector<string>> Parse::parseCSV(string file)
{
// input fstream instance
ifstream inFile;
inFile.open(file);

// check for error
if (inFile.fail()) { cerr << "Cannot open file" << endl; exit(1); }

vector<vector<string>> data;
string line;

while (getline(inFile, line))
{
stringstream inputLine(line);
char delimeter = ',';
string word;
vector<string> brokenLine;
while (getline(inputLine, word, delimeter)) {
word.erase(remove(word.begin(), word.end(), ' '), word.end()); // remove all white spaces
brokenLine.push_back(word);
}
data.push_back(brokenLine);
}

inFile.close();

return data;

};

这是十六进制转储。我不确定这到底显示了什么。

0000000 55 4e 49 58 20 54 49 4d 45 2c 54 49 4d 45 2c 4c
0000010 41 54 2c 4c 4f 4e 47 2c 41 4c 54 2c 44 49 53 54
0000020 2c 48 52 2c 43 41 44 2c 54 45 4d 50 2c 50 4f 57
0000030 45 52 0d 31 34 32 34 31 30 35 38 30 38 2c 32 30
0000040 31 35 2d 30 32 2d 31 36 54 31 36 3a 35 36 3a 34
0000050 38 5a 2c 34 33 2e 38 39 36 34 2c 31 30 2e 32 32
0000060 34 34 34 2c 30 2e 38 37 2c 30 2c 30 2c 30 2c 4e
0000070 6f 20 44 61 74 61 2c 4e 6f 20 44 61 74 61 0d 31
0000080 34 32 34 31 30 35 38 38 35 2c 32 30 31 35 2d 30
0000090 32 2d 31 36 54 31 36 3a 35 38 3a 30 35 5a 2c 34
00000a0 33 2e 39 30 31 33 35 2c 31 30 2e 32 32 30 34 31
00000b0 2c 31 2e 30 32 2c 30 2e 36 33 39 2c 30 2c 30 2c
00000c0 4e 6f 20 44 61 74 61 2c 4e 6f 20 44 61 74 61 0d
00000d0 31 34 32 34 31 30 35 38 38 38 2c 32 30 31 35 2d
00000e0 30 32 2d 31 36 54 31 36 3a 35 38 3a 30 38 5a 2c
00000f0 34 33 2e 39 30 31 34 38 2c 31 30 2e 32 32 30 31
0000100

文件的前两行

UNIX TIME,TIME,LAT,LONG,ALT,DIST,HR,CAD,TEMP,POWER
1424105808,2015-02-16T16:56:48Z,43.8964,10.22444,0.87,0,0,0,No Data,No Data

更新 看起来是\r。我不确定为什么它没有更早地工作,但我在探索的过程中学到了一些东西。感谢大家的帮助。

最佳答案

一个简单的解决方法是编写您自己的 getline
例如忽略 \n,\r
的任意组合在行的开头,也打破了任何。
这适用于任何平台,但不会保留空行。

查看十六进制转储后,分隔符为0d (\r)

关于c++ - 解析具有 CR LF EOL 结构的 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28790266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com