How to split a csv file
-
wrote on 6 Jun 2021, 17:41 last edited by
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( "\",\"");
-
wrote on 6 Jun 2021, 20:35 last edited by
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.
Leopoldwrote on 6 Jun 2021, 21:32 last edited by JonB 6 Jun 2021, 21:33@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. -
wrote on 7 Jun 2021, 02:12 last edited by Kent-Dorfman 6 Jul 2021, 02:14
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".
-
Lifetime Qt Championwrote on 7 Jun 2021, 05:49 last edited by SGaist 6 Jul 2021, 06:18
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.
-
wrote on 7 Jun 2021, 06:47 last edited by SGaist 6 Jul 2021, 19:24
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
2/7