Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Regarding reading CSV file
QtWS25 Last Chance

Regarding reading CSV file

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 8.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    Hi All

    I have written a code to read the *.csv file and is working fine.Below is my code
    @
    QFile file(filename);
    QTextStream in(&file);
    if (file.open(QIODevice::ReadOnly))
    {
    QStringList listdata;
    QString line = in.readLine();
    listdata = line.split(',');
    int iHeaderCount = listdata.count();
    *HeaderCnt = iHeaderCount;
    while (!line.isNull())
    {
    line = in.readLine();
    line.remove(" ");
    listdata.append(line.split(','));
    }
    }
    @

    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?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        See "this thread":http://developer.qt.nokia.com/forums/viewthread/6893 and "this thread":http://developer.qt.nokia.com/forums/viewthread/7704 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!

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          [quote author="Volker" date="1316043097"]See "this thread":http://developer.qt.nokia.com/forums/viewthread/6893 and "this thread":http://developer.qt.nokia.com/forums/viewthread/7704 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![/quote]

          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.

          (Z(:^

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Indrajeet
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              [quote author="Rajveer" date="1316082148"]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.
              [/quote]

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

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • P Offline
                P Offline
                p-himik
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved