問題描述
我正在用 C++ 讀取 CSV 文件,行格式如下:
I'm reading a CSV file in C++ and the row format is as such:
小學,中學,第三",主要",,中學",18, 4, 0, 0, 0
"Primary, Secondary, Third", "Primary", , "Secondary", 18, 4, 0, 0, 0
(注意空值)
當我這樣做時:
while (std::getline(ss, csvElement, ',')) {
csvColumn.push_back(csvElement);
}
這會將第一個字符串分成不正確的部分.
This splits up the first string into pieces which isn't correct.
如何在迭代時保留字符串?我嘗試將上述方法結合起來,同時還抓取了由雙引號分隔的行,但我得到了瘋狂的結果.
How do I preserve the string when iterating? I tried to do a combination of the above and while also grabbing the lines separated by double quote but I got wild results.
推薦答案
您需要根據是否在引號之間來解釋逗號.這對于 getline()
來說太復雜了.
You need to interpret the comma depending on whether you're betwwen the quote or not. This is too complexfor getline()
.
解決方案是使用 getline()
讀取整行,并通過逐個字符遍歷字符串來解析該行,并維護一個指示符是否在雙引號之間.
The solution would be to read the full line with getline()
, and parse the line by iterating through the string character by character, and maintaing an indicator whether you're between double quotes or not.
這是第一個原始"示例(未刪除字段中的雙引號且不解釋轉義字符):
Here is a first "raw" example (double quotes are not removed in the fields and escape characters are not interpreted):
string line;
while (std::getline(cin, line)) { // read full line
const char *mystart=line.c_str(); // prepare to parse the line - start is position of begin of field
bool instring{false};
for (const char* p=mystart; *p; p++) { // iterate through the string
if (*p=='"') // toggle flag if we're btw double quote
instring = !instring;
else if (*p==',' && !instring) { // if comma OUTSIDE double quote
csvColumn.push_back(string(mystart,p-mystart)); // keep the field
mystart=p+1; // and start parsing next one
}
}
csvColumn.push_back(string(mystart)); // last field delimited by end of line instead of comma
}
在線演示
這篇關于帶有逗號和雙引號內的字符串的 C++ CSV 行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!