Home » U++ Library support » U++ Core » GetCsvLine (Reading CSVfile)
Re: GetCsvLine [message #50791 is a reply to message #49987] |
Wed, 19 December 2018 09:31 |
bozero
Messages: 20 Registered: June 2018
|
Promising Member |
|
|
Thank you for your reply and nice example, Oblivion. But my focus on a string with zero length ("") before new line, GetCsvLine() seems just ignore it. Even though a pair double double quotation ("") is put as space holder between (,) and (\n). Shall I need to put extra (,) to end of the each line of CSV before call GetCsvLine ?
void getcsvline_vs_split()
{
StdLogSetup(LOG_COUT|LOG_FILE);
const String csvstr = "data1,data2,\"data3\"\ndata4,data5,\"\"\ndata7,data8,\"\ ",";
DUMP(csvstr);
Cout()<<"*** Get data via GetCSVLine ***\n";
StringStream ss(csvstr);
int ln = 0;
int total=0;
while(!ss.IsEof()) {
auto s = ss.GetLine();
StringStream line(s);
auto v = GetCsvLine(line, ',', CHARSET_DEFAULT);
Cout() << "Line: " << ++ln << "\n";
for(auto e : v)
Cout() << "Item " << ++total << " : " << e << '\n';
}
Cout()<< "Total line: " << ln << "\nTotal item: " << total << "\n";
Cout()<<"*** Get data via Split ***\n";
ln = 0;
total=0;
ss.Open(csvstr);
while(!ss.IsEof()) {
auto s=Split(ss.GetLine(),',');
Cout() << "Line: " << ++ln << "\n";
for(auto e : s)
Cout() << "Item " << ++total << " : " << e << '\n';
}
Cout()<< "Total line: " << ln << "\nTotal item: " << total << "\n";
}
output:
csvstr = data1,data2,"data3" // string with ""
data4,data5,"" // empty string
data7,data8,"", // extra comma at the end of line
*** Get data via GetCSVLine ***
Line: 1
Item 1 : data1
Item 2 : data2
Item 3 : data3 // string with "" is stripped
Line: 2
Item 4 : data4
Item 5 : data5 // expected empty string item "" is miss
Line: 3
Item 6 : data7
Item 7 : data8
Item 8 : // extra comma append for empty string item ""
Total line: 3
Total item: 8
*** Get data via Split *** // just compare with split
Line: 1
Item 1 : data1
Item 2 : data2
Item 3 : "data3" // string with ""
Line: 2
Item 4 : data4
Item 5 : data5
Item 6 : "" // string with ""
Line: 3
Item 7 : data7
Item 8 : data8
Item 9 : "" // string with ""
Total line: 3
Total item: 9
[Updated on: Wed, 19 December 2018 09:33] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Tue May 21 17:15:00 CEST 2024
Total time taken to generate the page: 0.02221 seconds
|