C ++ CSV行,带有双引号中的逗号和字符串 [英] C++ CSV line with comas and strings within double quotes

查看:886
本文介绍了C ++ CSV行,带有双引号中的逗号和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++读取CSV档案,其格式如下:

I'm reading a CSV file in C++ and the row format is as such:

「Primary,Secondary,Third」,「Primary」, ,18,4,0,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.

这里是第一个raw示例(字段中不删除双引号,不解释转义字符):

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行,带有双引号中的逗号和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆