我正在用 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模板网!
如何在 C++ 中读取和操作 CSV 文件数据?How can I read and manipulate CSV file data in C++?(如何在 C++ 中读取和操作 CSV 文件数据?)
在 C++ 中,为什么我不能像这样编写 for() 循环:In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,为什么我不能像这样编写 for() 循环: for(
OpenMP 如何处理嵌套循环?How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
在循环 C++ 中重用线程Reusing thread in loop c++(在循环 C++ 中重用线程)
需要精确的线程睡眠.最大 1ms 误差Precise thread sleep needed. Max 1ms error(需要精确的线程睡眠.最大 1ms 误差)
是否需要“do {...} while ()"?环形?Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?环形?)