久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 boost::spirit 解析 csv

How to parse csv using boost::spirit(如何使用 boost::spirit 解析 csv)
本文介紹了如何使用 boost::spirit 解析 csv的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有這個 csv 行

std::string s = R"(1997,Ford,E350,"ac, abs, moon","some "rusty" parts",3000.00)";

我可以使用 boost::tokenizer 解析它:

I can parse it using boost::tokenizer:

typedef boost::tokenizer< boost::escaped_list_separator<char> , std::string::const_iterator, std::string> Tokenizer;
boost::escaped_list_separator<char> seps('\', ',', '"');
Tokenizer tok(s, seps);
for (auto i : tok)
{
    std::cout << i << std::endl;
}

它是正確的,除了標記生銹"應該有雙引號被剝離.

It gets it right except token "rusty" should have double quotes which are getting stripped.

這是我嘗試使用 boost::spirit

Here is my attempt to use boost::spirit

boost::spirit::classic::rule<> list_csv_item = !(boost::spirit::classic::confix_p('"', *boost::spirit::classic::c_escape_ch_p, '"') | boost::spirit::classic::longest_d[boost::spirit::classic::real_p | boost::spirit::classic::int_p]);
std::vector<std::string> vec_item;
std::vector<std::string>  vec_list;
boost::spirit::classic::rule<> list_csv = boost::spirit::classic::list_p(list_csv_item[boost::spirit::classic::push_back_a(vec_item)],',')[boost::spirit::classic::push_back_a(vec_list)];
boost::spirit::classic::parse_info<> result = parse(s.c_str(), list_csv);
if (result.hit)
{
  for (auto i : vec_item)
  {
    cout << i << endl;
   }
}

問題:

  1. 不起作用,只打印第一個標記

  1. does not work, prints the first token only

為什么是 boost::spirit::classic?找不到使用 Spirit V2 的示例

why boost::spirit::classic? can't find examples using Spirit V2

設置很殘酷..但我可以忍受

the setup is brutal .. but I can live with this

** 我真的很想使用 boost::spirit 因為它往往很快

** I really want to use boost::spirit because it tends to be pretty fast

預期輸出:

1997
Ford
E350
ac, abs, moon
some "rusty" parts

3000.00

推薦答案

Sehe 的帖子看起來比我的要干凈一些,但我把它放在一起了一段時間,所以無論如何都在這里:

Sehe's post looks a fair bit cleaner than mine, but I was putting this together for a bit, so here it is anyways:

#include <boost/tokenizer.hpp>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

int main() {
    const std::string s = R"(1997,Ford,E350,"ac, abs, moon",""rusty"",3000.00)";

    // Tokenizer
    typedef boost::tokenizer< boost::escaped_list_separator<char> , std::string::const_iterator, std::string> Tokenizer;
    boost::escaped_list_separator<char> seps('\', ',', '"');
    Tokenizer tok(s, seps);
    for (auto i : tok)
        std::cout << i << "
";
    std::cout << "
";

    // Boost Spirit Qi
    qi::rule<std::string::const_iterator, std::string()> quoted_string = '"' >> *(qi::char_ - '"') >> '"';
    qi::rule<std::string::const_iterator, std::string()> valid_characters = qi::char_ - '"' - ',';
    qi::rule<std::string::const_iterator, std::string()> item = *(quoted_string | valid_characters );
    qi::rule<std::string::const_iterator, std::vector<std::string>()> csv_parser = item % ',';

    std::string::const_iterator s_begin = s.begin();
    std::string::const_iterator s_end = s.end();
    std::vector<std::string> result;

    bool r = boost::spirit::qi::parse(s_begin, s_end, csv_parser, result);
    assert(r == true);
    assert(s_begin == s_end);

    for (auto i : result)
        std::cout << i << std::endl;
    std::cout << "
";
}   

這輸出:

1997
Ford
E350
ac, abs, moon
rusty
3000.00

1997
Ford
E350
ac, abs, moon
rusty
3000.00

值得注意的事情:這沒有實現完整的 CSV 解析器.您還需要研究轉義字符或其他實現所需的任何內容.

Something Worth Noting: This doesn't implement a full CSV parser. You'd also want to look into escape characters or whatever else is required for your implementation.

另外:如果您正在查看文檔,那么您就會知道,在 Qi 中,'a' 等效于 boost::spirit::qi::lit('a')"abc" 等價于 boost::spirit::qi::lit("abc").

Also: If you're looking into the documentation, just so you know, in Qi, 'a' is equivalent to boost::spirit::qi::lit('a') and "abc" is equivalent to boost::spirit::qi::lit("abc").

關于雙引號:因此,正如 Sehe 在上面的評論中指出的那樣,輸入文本中圍繞 "" 的規則并不直接清楚意味著什么.如果您希望所有不在帶引號的字符串中的 "" 實例都轉換為 ",那么類似下面的內容將起作用.

On Double quotes: So, as Sehe notes in a comment above, it's not directly clear what the rules surrounding a "" in the input text means. If you wanted all instances of "" not within a quoted string to be converted to a ", then something like the following would work.

qi::rule<std::string::const_iterator, std::string()> double_quote_char = """" >> qi::attr('"');
qi::rule<std::string::const_iterator, std::string()> item = *(double_quote_char | quoted_string | valid_characters );

這篇關于如何使用 boost::spirit 解析 csv的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: av电影一区二区 | 国产精品久久久久久52avav | av特级毛片 | 亚洲精品99| 91亚洲免费 | 成人性视频免费网站 | 麻豆久久久久久久久久 | 蜜桃精品视频在线 | 91久久夜色精品国产网站 | 精品一区二区三区入口 | 蜜桃精品噜噜噜成人av | 男人天堂999 | 欧美一区 | 亚洲精品久久国产高清情趣图文 | 丁香久久 | 国产精品免费在线 | 欧美精品影院 | 国产日韩一区二区 | 最新国产精品精品视频 | 四虎影音 | 国产精品资源在线 | 亚洲国产精品久久人人爱 | 国产成人精品一区二区三区在线 | 日产精品久久久一区二区福利 | 精品一区二区三区四区在线 | 黄视频网址 | 欧美精品首页 | 国产精品久久久久久久久久免费看 | 小川阿佐美pgd-606在线 | 国产精品视频网 | 中文字幕日韩一区二区 | 黑人巨大精品 | 国产午夜影院 | 国产成人久久av免费高清密臂 | 性精品 | 成年人在线播放 | 91精品久久久久久综合五月天 | 日本网站免费在线观看 | 91久久精品国产91久久性色tv | 日日摸日日添日日躁av | 一区二区免费视频 |