gpt4 book ai didi

c++ - 使用 token 仅解析 csv 文件中的特定列

转载 作者:行者123 更新时间:2023-11-30 02:44:26 25 4
gpt4 key购买 nike

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

"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

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

最佳答案

使用专用库来完成此任务要容易得多。用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<std::string> 和以后的输出 带计数器的循环),即使是您自制的字符串拆分算法。基本思想是一样的:

std::vector<std::string> :

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());
}

关于c++ - 使用 token 仅解析 csv 文件中的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25326889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com