【问题标题】:How can i add a delimeter to boost::escaped_list_separator?如何在 boost::escaped 列表分隔符中添加分隔符?
【发布时间】:2022-01-04 13:07:38
【问题描述】:

我不复制字符串以稍后修剪它。 我解析 csv 文件,我的代码:

while(std::getline(stream, line))
    {
        boost::tokenizer<boost::escaped_list_separator<char>> tok(line);
        std::for_each(tok.begin(), tok.end(), handler);
        
    }

parseCSV(file, [](const std::string& tok)
    {
        std::vector<SpiceSimulation::DataVector*> arrays;
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        std::cout << "\t-->" << tok << std::endl;
        //std::string cptoken = boost::trim_copy(tok);
        //Read Header Titles
        if(boost::starts_with(tok, "v"))
        {
            std::cout << "START WITH\n";
        }
        
    }); 

我的文件.csv:

time, vtime2, vtime3, vtime4 ...   

我得到带有空格的结果 结果:["time","vtime2","vtime3","vtime4"]

如何在不复制的情况下消除这些空格?如果我将正确的标记器返回结果理解为 basic_string 它不是原始字符串的副本

【问题讨论】:

标签: c++ boost


【解决方案1】:

tokenizer 函数有构造函数

explicit escaped_list_separator(Char  e = '\\',
                                Char c = ',',Char  q = '\"')
  : escape_(1,e), c_(1,c), quote_(1,q), last_(false) { }

escaped_list_separator(string_type e, string_type c, string_type q)
  : escape_(e), c_(c), quote_(q), last_(false) { }

可以通过这些:

    boost::escaped_list_separator<char> tf("\\", ", ", "\"");
    boost::tokenizer<boost::escaped_list_separator<char>> tok(line, tf);
    std::for_each(tok.begin(), tok.end(), handler);

但它并不完全符合您的预期:

Line: "time, vtime2, vtime3, vtime4 ...   "
        -->"time"
        -->""
        -->"vtime2"
START WITH
        -->""
        -->"vtime3"
START WITH
        -->""
        -->"vtime4"
START WITH
        -->"..."
        -->""
        -->""
        -->""

我会以另一种方式做到这一点。解析!= 标记化。参见例如https://stackoverflow.com/search?tab=newest&q=user%3a85371%20csv%20parser

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2021-01-04
    • 2013-06-09
    相关资源
    最近更新 更多