September 14, 2011

Rajveer Rajveer
Hobby Entomologist
186 posts

Regarding reading CSV file

 

Hi All

I have written a code to read the *.csv file and is working fine.Below is my code

  1.  QFile file(filename);
  2.  QTextStream in(&file);
  3.  if (file.open(QIODevice::ReadOnly))
  4.  {
  5.         QStringList listdata;
  6.         QString line = in.readLine();
  7.         listdata = line.split(',');
  8.         int iHeaderCount = listdata.count();
  9.        *HeaderCnt = iHeaderCount;
  10.          while (!line.isNull())
  11.          {
  12.   line = in.readLine();
  13.   line.remove(" ");
  14.   listdata.append(line.split(','));
  15.          }
  16.             }

But when my data in *.csv file is as below

“1”,“2”,“3”,“4”,“11,22,33”,“4”,“5”,“6”,“7”

I want to read 11,22,33 as one value.How to do that?

 Signature 

Regards
Rajveer

6 replies

September 14, 2011

sierdzio sierdzio
Dinosaur Breeder
536 posts

If you’ve got control over CSV file itself, the easiest way would be, of course, to use another delimiter (like ‘;’), then reading would be pretty straightforward.

Here, if you want to check for those 11,22,33 sequences AFTER splitting, you can check the resulting strings for ‘”’ character. A lack of at leas one would indicate, that you are inside the sequence. This does not seem to be a very elegant way, though.

If you want to check BEFORE splitting, you might want to consider using ‘”’ as the delimiter – this way you would get loads of rubbish results (’,’), but they can be easily filtered out.

Or, as probably the easiest route, you can simply use “,” (quote, comma, quote) as your delimiter.

 Signature 

(N(:^

September 15, 2011

Volker Volker
Mad Scientist
5250 posts

See this thread [developer.qt.nokia.com] and this thread [developer.qt.nokia.com] for further reference and possible solutions.

You should avoid writing your own CSV parser if it is by any means possible for you to use an existing solution!

September 15, 2011

sierdzio sierdzio
Dinosaur Breeder
536 posts

Volker wrote:
See this thread [developer.qt.nokia.com] and this thread [developer.qt.nokia.com] for further reference and possible solutions.

You should avoid writing your own CSV parser if it is by any means possible for you to use an existing solution!

Rajveer, judging from Volker’s links, you’ve already asked about CSV parsing twice, and been given elaborate and instructive responses every time. Try being a bit more considerate about other peoples’ time in the future, please.

 Signature 

(N(:^

September 15, 2011

Rajveer Rajveer
Hobby Entomologist
186 posts

Hi sierdzio

I have asked the question twice but both the time it was not same.
And now also i have some specific doubt about parsing csv not the repeated doubt.

 Signature 

Regards
Rajveer

September 17, 2011

Volker Volker
Mad Scientist
5250 posts

Rajveer wrote:
Hi sierdzio

I have asked the question twice but both the time it was not same.
And now also i have some specific doubt about parsing csv not the repeated doubt.

As you could guess yourself, the advice stays the same: take a ready-to-use solution and avoid coding one yourself.

September 18, 2011

p-himik p-himik
Robot Herder
254 posts

Maybe this will help: http://libqxt.bitbucket.org/doc/tip/qxtcsvmodel.html

 
  ‹‹ Toolbar only shows button widgets, under the popup, which appears when there is not enough space available for docked toolbar [Solved]      standard selection ››

You must log in to post a reply. Not a member yet? Register here!