【问题标题】:Parse only specific columns from csv file using token使用令牌仅解析 csv 文件中的特定列
【发布时间】:2014-10-09 05:12:22
【问题描述】:

如果我有一个用逗号分隔值填充的文件,例如:

"myComputer",5,192.168.1.0,25
"herComputer",6,192.168.1.1,26
"hisComputer",7,192.168.1.2,27

我想将数据作为字符串提取出来,我会这样做:

std::string line;
std::ifstream myfile ("myCSVFile.txt");

if(myfile.is_open())
{
    while(getline(myfile,line))
    {
        std::string tempString = line;
        std::string delimiter = ",";
    }
}

为了自己解析每个值,我使用了这样的东西:Parse (split) a string in C++ using string delimiter (standard C++)

std::string s = "scott>=tiger>=mushroom";
std::string delimiter = ">=";

size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
    token = s.substr(0, pos);
    std::cout << token << std::endl;
    s.erase(0, pos + delimiter.length());
}
std::cout << s << std::endl;

问题是,如果我只想要第一个和第三个值怎么办?所以如果我想要我的 csv 文件从上面只输出

"myComputer" 192.168.1.0
"herComputer" 192.168.1.1
"hisComputer" 192.168.1.2

有没有办法使用上述方法来实现这一点,或者我应该使用完全不同的方法?谢谢,

【问题讨论】:

  • 在while循环中使用计数器计算列号,只输出你想要的列?还是我错过了什么?
  • 只是..不要输出第二列....
  • 逗号不一定是分隔符。特别是,如果您在带引号的字符串中找到逗号,则它不是分隔符。你的方法假设不是这样。
  • 是的,它确实假设不是这样。对于这个问题,我们假设逗号只会用作分隔符。

标签: c++ file parsing csv ifstream


【解决方案1】:

为此任务使用专用库要容易得多。使用Boost Tokenizer's Escaped List Separator,轻而易举:

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <boost/tokenizer.hpp>

int main()
{
    std::ifstream myfile("myCSVFile.txt");

    if (myfile.is_open())
    {
        std::string line;
        while (std::getline(myfile, line))
        {
            typedef boost::escaped_list_separator<char> Separator;
            typedef boost::tokenizer<Separator> Tokenizer;

            std::vector<std::string> tokens;
            Tokenizer tokenizer(line);
            for (Tokenizer::iterator iter = tokenizer.begin(); iter != tokenizer.end(); ++iter)
            {
               tokens.push_back(*iter);
            }

            if (tokens.size() == 4)
            {
                std::cout << tokens[0] << "\t" << tokens[2] << "\n";
            }
            else
            {
                std::cerr << "illegal line\n";
            }
        }
    }
}

请注意,在 C++11 中,您可以简化循环:

for (auto &token : tokenizer)
{
    tokens.push_back(token);
}          

如您所见,我们的想法是将一行的所有值存储在 std::vector 中,然后输出所需的内容。

现在,如果您真的处理大文件,这可能会导致性能问题。在这种情况下,将计数器与分词器一起使用:

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <boost/tokenizer.hpp>

int main()
{
    std::ifstream myfile("myCSVFile.txt");

    if (myfile.is_open())
    {
        std::string line;
        while (std::getline(myfile, line))
        {
            typedef boost::escaped_list_separator<char> Separator;
            typedef boost::tokenizer<Separator> Tokenizer;

            Tokenizer tokenizer(line);
            int count = 0;
            for (Tokenizer::iterator iter = tokenizer.begin(); (iter != tokenizer.end()) && (count < 3); ++iter)
            {
                if ((count == 0) || (count == 2))
                {
                    std::cout << *iter;
                    if (count == 0)
                    {
                        std::cout << "\t";
                    }
                }
                ++count;
            }
            std::cout << "\n";
        }
    }
}

即使使用自制的字符串拆分算法,您也可以使用这两种技术(std::vector&lt;std::string&gt; 稍后输出 或 带计数器的循环)。基本思路是一样的:

std::vector&lt;std::string&gt;:

std::vector<std::string> tokens;
while ((pos = s.find(delimiter)) != std::string::npos) {
    token = s.substr(0, pos);
    tokens.push_back(token);
    s.erase(0, pos + delimiter.length());
}

if (tokens.size() == 4)
{
    std::cout << tokens[0] << "\t" << tokens[2] << "\n";
}
else
{
    std::cerr << "illegal line\n";
}

带计数器:

int count = 0;
while ((pos = s.find(delimiter)) != std::string::npos && (count < 4)) {
    token = s.substr(0, pos);

    if ((count == 0) || (count == 2))
    {
        std::cout << token;
        if (count == 0)
        {
            std::cout << "\t";
        }
    }
    ++count;
    s.erase(0, pos + delimiter.length());
}

【讨论】:

  • 感谢您回答问题,并提供更优雅的解决方案。
【解决方案2】:

正如上面的 cmets 建议的那样,答案是只输出我想要的列。我通过添加一个带有执行实际打印的循环的计数器来实现它。我本可以轻松地将计数器添加到 for 循环中,而不是一会儿,但我没有。

std::string s = "scott>=tiger>=mushroom";
std::string delimiter = ">=";

size_t pos = 0;
std::string token;

int counter = 0;
while ((pos = s.find(delimiter)) != std::string::npos) 
{
    token = s.substr(0, pos);

    if(counter == 0 || counter == 2)
    {
        std::cout << token << std::endl;
    }


    s.erase(0, pos + delimiter.length());
}

奇怪的是,我错误地思考了这个问题,“只打印你想要的那些”这个非常简单的 cmets 确实有帮助。谢谢

【讨论】:

    猜你喜欢
    • 2019-09-27
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 2017-05-14
    • 2020-09-08
    • 1970-01-01
    相关资源
    最近更新 更多