How to split a csv file
-
Hello,
what are the delimiters to split a csv file.
The file looks like: "ABC","DEF","GHI","JKL"
when I split with line.split(",") my new line looks the same.
line .split ("","") will not be accepted.
I want ABC , DEF , GHI , JKLThank you for the help.
Leopold -
Hi
To use " you must escape ittest.split( "\",\"");
-
Hi mrjj,
sorry but not the solution.
Here is part of the file:```
252,"0",,,"0.006","0.058","32.9","13.968",,,,"true","true","true","true","true","252","100",,"1","7","8","0","0","34","103.04","1984","32.9",,"0",,,"0","4.81","0"code_textand here is the debug output:``` Zeile2181 line: "252,\"0\",,,\"0.006\",\"0.058\",\"32.9\",\"13.968\",,,,\"true\",\"true\",\"true\",\"true\",\"true\",\"252\",\"100\",,\"1\",\"7\",\"8\",\"0\",\"0\",\"34\",\"103.04\",\"1984\",\"32.9\",,\"0\",,,\"0\",\"4.81\",\"0\""
possibly the missing quotation marks in the first column.
Leopold -
Hi mrjj,
sorry but not the solution.
Here is part of the file:```
252,"0",,,"0.006","0.058","32.9","13.968",,,,"true","true","true","true","true","252","100",,"1","7","8","0","0","34","103.04","1984","32.9",,"0",,,"0","4.81","0"code_textand here is the debug output:``` Zeile2181 line: "252,\"0\",,,\"0.006\",\"0.058\",\"32.9\",\"13.968\",,,,\"true\",\"true\",\"true\",\"true\",\"true\",\"252\",\"100\",,\"1\",\"7\",\"8\",\"0\",\"0\",\"34\",\"103.04\",\"1984\",\"32.9\",,\"0\",,,\"0\",\"4.81\",\"0\""
possibly the missing quotation marks in the first column.
Leopold@Leopold
The only correct way is to parse it left to right. Not justsplit()
.If you do want to split: the "correct" split character is
,
. Then you strip the"
s off the resulting token if they are present. This works nicely so long as nothing has an embedded,
, which may work for you. If you want to handle that correctly you would need a regular expression. But like I said the properly robust way is to scan the string left to right. -
proper parsing for all use cases would require a lexical analysis using regular expressions to tokenize and parse the expected grammar. Otherwise you cannot handle special cases of embedded quotes or commas within the fields themselves.
I'd say it's time to read the "Dragon Book".
-
Hi
Sorry i though you asked about splitting with "Anyway
IF
1: no , (comma) in any the data we want to keep
2: no " in any the data we want to keepthen you could just do like
const char* s1 = R"foo("252,"0",,,"0.006","0.058","32.9","13.968",,,,"true","true","true","true","true","252","100","1","7","8","0","0","34","103.04","1984","32.9",,"0",,,"0","4.81","0")foo"; QString clean(s1); clean.replace("\"", ""); // get rid of " QStringList all = clean.split(","); // split at commas for (QString& str : all) { qDebug().noquote() << str; // notice the noquote as else qDebug shows with "" which could confuse us }
252 0 0.006 0.058 32.9 13.968 true true true true true 252 100 1 7
That said. Both @JonB and @Kent-Dorfman are right. using split only works good if the data is uniform and contains no surprices.
-
Hello,
very easy when you know how it works.
Here is my code.QString line = in.readLine(); qDebug() << "Zeile2181 line:"<<line; line.replace("\"", ""); // get rid of " QStringList fields = line.split(","); // split at commas qDebug().noquote() <<"zeile 2184 fields"<< fields; // notice the noquote as else qDebug shows with "" which could confuse us x.append(fields.at(0).toDouble());
Thank you mrjj,JohnB and Kent
Leopold