I have a problem with converting any string from CSV into string (but not string of char) and then tokenize it.
There is my code here:
#include <iostream>
#include <math.h>
#include "NumCpp.hpp"
#include <cstdlib>
#include <python3.10/Python.h>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
typedef tokenizer< escaped_list_separator<char> > Tokenizer;
//Take this advice from one site
int main()
{
string data("DATA.csv");
ifstream in(data.c_str());
while (getline(in, line))
{
Tokenizer tok(line);
for (tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg) {
cout << *beg << "\n";
}
}
return 0;
}
It's just copy strings from CSV file one by one.
I don't know how to control the tokenize symbol of this function. In official documentation I had only found a little piece of code, which works only with your string variable..
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>
int main() {
using namespace std;
using namespace boost;
string s = "This is, a test";
tokenizer<> tok(s);
for (tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg) {
cout << *beg << "\n";
}
}
The output from simple_example_1 is: Live
This
is
a
test
I accepting advice from you about different arguments of tokenizer, and how I can solve my tokenize reading from csv.
First off, don't (ever) do this:
It leads to at least a dozen name conflicts. In general, avoid
using namespace
.The Question
You're using
Tokenizer
as:This means it uses
escaped_list_separator
as the separator. You can use other than the default constructors to pass initializers to it:E.g.
Full sample: Live
If you don't want/need the escaping logic, use another separator class, e.g. https://www.boost.org/doc/libs/1_63_0/libs/tokenizer/char_separator.htm
Further Reading
Sometimes tokenizing is just not enough. Consider writing a parser: