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. How to split a csv file

How to split a csv file

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.5k 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.
  • L Offline
    L Offline
    Leopold
    wrote on 6 Jun 2021, 17:41 last edited by
    #1

    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 , JKL

    Thank you for the help.
    Leopold

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 Jun 2021, 17:50 last edited by
      #2

      Hi
      To use " you must escape it

      test.split( "\",\"");
      
      1 Reply Last reply
      1
      • L Offline
        L Offline
        Leopold
        wrote on 6 Jun 2021, 20:35 last edited by
        #3

        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_text

        
        and 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

        J 1 Reply Last reply 6 Jun 2021, 21:32
        0
        • L Leopold
          6 Jun 2021, 20:35

          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_text

          
          and 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

          J Offline
          J Offline
          JonB
          wrote on 6 Jun 2021, 21:32 last edited by JonB 6 Jun 2021, 21:33
          #4

          @Leopold
          The only correct way is to parse it left to right. Not just split().

          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.

          1 Reply Last reply
          4
          • K Offline
            K Offline
            Kent-Dorfman
            wrote on 7 Jun 2021, 02:12 last edited by Kent-Dorfman 6 Jul 2021, 02:14
            #5

            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".

            1 Reply Last reply
            1
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 7 Jun 2021, 05:49 last edited by SGaist 6 Jul 2021, 06:18
              #6

              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 keep

              then 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.

              1 Reply Last reply
              3
              • L Offline
                L Offline
                Leopold
                wrote on 7 Jun 2021, 06:47 last edited by SGaist 6 Jul 2021, 19:24
                #7

                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

                1 Reply Last reply
                1

                2/7

                6 Jun 2021, 17:50

                topic:navigator.unread, 5
                • Login

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