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 i can identify and read the columns of a csv file ?
Forum Updated to NodeBB v4.3 + New Features

how i can identify and read the columns of a csv file ?

Scheduled Pinned Locked Moved Solved General and Desktop
49 Posts 8 Posters 4.4k Views 3 Watching
  • 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.
  • J julie546

    how i can identify and read the columns of a csv file ?
    I managed to open the file and identify the first column by : QStringList coordXList;
    while (!filelidar.atEnd()) {
    QByteArray line = filelidar.readLine();
    coordXList.append(line.split(';').first());
    QStringList coordXList;
    }

            qDebug() << coordXList;
    

    and I couldn't find how I can identify and read and the other columns how can i do it please

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @julie546 said in how i can identify and read the columns of a csv file ?:

    and I couldn't find how I can identify and read and the other columns how can i do it please

    split() gives you a list where you can access elements by their index. So, what is exactly the problem?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • J julie546

      how i can identify and read the columns of a csv file ?
      I managed to open the file and identify the first column by : QStringList coordXList;
      while (!filelidar.atEnd()) {
      QByteArray line = filelidar.readLine();
      coordXList.append(line.split(';').first());
      QStringList coordXList;
      }

              qDebug() << coordXList;
      

      and I couldn't find how I can identify and read and the other columns how can i do it please

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #3

      @julie546
      And additonal to @jsulm you are splitting on ;, normally a CSV file elements are separated by ,?

      J.HilkJ J 2 Replies Last reply
      0
      • JonBJ JonB

        @julie546
        And additonal to @jsulm you are splitting on ;, normally a CSV file elements are separated by ,?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #4

        @JonB said in how i can identify and read the columns of a csv file ?:

        @julie546
        And additonal to @jsulm you are splitting on ;, normally a CSV file elements are separated by ,?

        the default depends on your region, really.
        Some languages use , as decimal point, than its usually ; as a separator


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        JonBJ 1 Reply Last reply
        2
        • JonBJ JonB

          @julie546
          And additonal to @jsulm you are splitting on ;, normally a CSV file elements are separated by ,?

          J Offline
          J Offline
          julie546
          wrote on last edited by
          #5

          @JonB @J-Hilk no for my case it is;
          @jsulm I can only read the first column but I would need the 2nd and 3rd

          jsulmJ JonBJ 2 Replies Last reply
          0
          • J julie546

            @JonB @J-Hilk no for my case it is;
            @jsulm I can only read the first column but I would need the 2nd and 3rd

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #6

            @julie546 said in how i can identify and read the columns of a csv file ?:

            I can only read the first column but I would need the 2nd and 3rd

            Then read them. What is the problem, really?

            QStringList elements = line.split(';');
            //elements[0] -> first element ("column")
            //elements[1] -> second element
            ...
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @JonB said in how i can identify and read the columns of a csv file ?:

              @julie546
              And additonal to @jsulm you are splitting on ;, normally a CSV file elements are separated by ,?

              the default depends on your region, really.
              Some languages use , as decimal point, than its usually ; as a separator

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              @J-Hilk said in how i can identify and read the columns of a csv file ?:

              Some languages use , as decimal point, than its usually ; as a separator

              Gosh, I didn't realize that (the changed separator, and the comma in numbers being what is in the file). So a CSV file containing only numbers, say, is not portable across regions? I assumed they would export/import locale-independent, like C locale. This is bad!

              1 Reply Last reply
              0
              • J julie546

                how i can identify and read the columns of a csv file ?
                I managed to open the file and identify the first column by : QStringList coordXList;
                while (!filelidar.atEnd()) {
                QByteArray line = filelidar.readLine();
                coordXList.append(line.split(';').first());
                QStringList coordXList;
                }

                        qDebug() << coordXList;
                

                and I couldn't find how I can identify and read and the other columns how can i do it please

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #8

                @julie546 said in how i can identify and read the columns of a csv file ?:

                how i can identify and read the columns of a csv file ?

                I would suggest you to give qtcsv a try.
                This should simplify working with CSV files

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                2
                • J julie546

                  @JonB @J-Hilk no for my case it is;
                  @jsulm I can only read the first column but I would need the 2nd and 3rd

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @julie546
                  @KroMignon has just recommended using qtcsv. I recommended same yesterday in post https://forum.qt.io/topic/128052/read-csv-file-values-and-put-these-values-in-a-graph/4 where someone asked about parsing CSV files. have a look at that, there are also other suggestions from a stackoverflow post (https://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-qt), depending on how complex your situation is.

                  J 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @julie546
                    @KroMignon has just recommended using qtcsv. I recommended same yesterday in post https://forum.qt.io/topic/128052/read-csv-file-values-and-put-these-values-in-a-graph/4 where someone asked about parsing CSV files. have a look at that, there are also other suggestions from a stackoverflow post (https://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-qt), depending on how complex your situation is.

                    J Offline
                    J Offline
                    julie546
                    wrote on last edited by julie546
                    #10

                    @JonB @KroMignon @jsulm @J-Hilk

                    thank you for your answer I was able to solve the problem with
                    coordXList.append (line.split (','). at (0));
                    coordYList.append (line.split (','). at (1));
                    but I would like to put these values ​​in a curve using the Qchart
                    I tried with
                    QLineSeries * series = new QLineSeries ();
                    series-> append (coordXList, coordYList);
                    but I got an error : no matching membre function for call to append
                    can you help me please

                    J.HilkJ KroMignonK 2 Replies Last reply
                    0
                    • J julie546

                      @JonB @KroMignon @jsulm @J-Hilk

                      thank you for your answer I was able to solve the problem with
                      coordXList.append (line.split (','). at (0));
                      coordYList.append (line.split (','). at (1));
                      but I would like to put these values ​​in a curve using the Qchart
                      I tried with
                      QLineSeries * series = new QLineSeries ();
                      series-> append (coordXList, coordYList);
                      but I got an error : no matching membre function for call to append
                      can you help me please

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

                      @julie546 said in how i can identify and read the columns of a csv file ?:

                      but I got an error : no matching membre function for call to append
                      can you help me please

                      of course,
                      QLineSeries has 3 overloads

                      • append(qreal , qreal )
                      • append(const QPointF &)
                      • append(const QList<QPointF> &)

                      non of those fit your append(Container<ofSomeType>, Container<ofSomeType>)


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      J 1 Reply Last reply
                      1
                      • J.HilkJ J.Hilk

                        @julie546 said in how i can identify and read the columns of a csv file ?:

                        but I got an error : no matching membre function for call to append
                        can you help me please

                        of course,
                        QLineSeries has 3 overloads

                        • append(qreal , qreal )
                        • append(const QPointF &)
                        • append(const QList<QPointF> &)

                        non of those fit your append(Container<ofSomeType>, Container<ofSomeType>)

                        J Offline
                        J Offline
                        julie546
                        wrote on last edited by julie546
                        #12

                        @J-Hilk so I can't display a graph from a csv file?

                        KroMignonK J.HilkJ 2 Replies Last reply
                        0
                        • J julie546

                          @JonB @KroMignon @jsulm @J-Hilk

                          thank you for your answer I was able to solve the problem with
                          coordXList.append (line.split (','). at (0));
                          coordYList.append (line.split (','). at (1));
                          but I would like to put these values ​​in a curve using the Qchart
                          I tried with
                          QLineSeries * series = new QLineSeries ();
                          series-> append (coordXList, coordYList);
                          but I got an error : no matching membre function for call to append
                          can you help me please

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #13

                          @julie546 said in how i can identify and read the columns of a csv file ?:

                          thank you for your answer I was able to solve the problem with
                          coordXList.append (line.split (','). at (0));
                          coordYList.append (line.split (','). at (1));

                          This is a dangerous and no effective way to do!
                          line.split(',') may return an empty QStringList, so line.split (','). at(0) will crash your application.
                          And why splitting twice?

                          I would do it like this:

                          const auto parts = line.split (',');
                          if(parts.size() >= 2)
                          {
                              coordXList.append(parts.at(0));
                              coordYList.append(parts.at(1));
                          }
                          

                          but I would like to put these values ​​in a curve using the Qchart
                          I tried with
                          QLineSeries * series = new QLineSeries ();
                          series-> append (coordXList, coordYList);
                          but I got an error : no matching membre function for call to append
                          can you help me please

                          Have you read QLineSeries::append() documentation??
                          There is not overload for QLineSeries::append(QStringList, QStringList)!

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          4
                          • J julie546

                            @J-Hilk so I can't display a graph from a csv file?

                            KroMignonK Offline
                            KroMignonK Offline
                            KroMignon
                            wrote on last edited by
                            #14

                            @julie546 said in how i can identify and read the columns of a csv file ?:

                            so I can't display a graph from a csv file?

                            Why not?
                            You have to convert your value to a compatible type. That's all.

                            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                            1 Reply Last reply
                            0
                            • J julie546

                              @J-Hilk so I can't display a graph from a csv file?

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by J.Hilk
                              #15

                              @julie546 thats not how programming works.

                              I assume coordXList and coordYList are string lists ?

                              than you may, and I emphasise may, get away with:

                              QLineSeries * series = new QLineSeries ();
                              for(int i{0}, j{qMin(coordXList.size(), coordYList.size())}; i<j; i++){
                                    series-> append (coordXList.at(i).toFloat(), coordYList.at(i).toFloat());
                              }
                              

                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              J 1 Reply Last reply
                              0
                              • J.HilkJ J.Hilk

                                @julie546 thats not how programming works.

                                I assume coordXList and coordYList are string lists ?

                                than you may, and I emphasise may, get away with:

                                QLineSeries * series = new QLineSeries ();
                                for(int i{0}, j{qMin(coordXList.size(), coordYList.size())}; i<j; i++){
                                      series-> append (coordXList.at(i).toFloat(), coordYList.at(i).toFloat());
                                }
                                
                                J Offline
                                J Offline
                                julie546
                                wrote on last edited by julie546
                                #16

                                @J-Hilk yes here is my output ("Y", "0.170138", "0.161089", "0.15901", "0.157898", "0.159706", "0.162481", "0.167204", "0.172876", "0.169802", "0.1677",.....................)
                                theres an error with series-> append (coordXList.at(i).toFloat(), coordYList.toFloat());
                                no member named toFloat in QStringList
                                @KroMignon @J-Hilk can you help me please I am a beginner c ++ I am a student in mechanics but I need qt for my end of study internship

                                J.HilkJ KroMignonK 2 Replies Last reply
                                0
                                • J julie546

                                  @J-Hilk yes here is my output ("Y", "0.170138", "0.161089", "0.15901", "0.157898", "0.159706", "0.162481", "0.167204", "0.172876", "0.169802", "0.1677",.....................)
                                  theres an error with series-> append (coordXList.at(i).toFloat(), coordYList.toFloat());
                                  no member named toFloat in QStringList
                                  @KroMignon @J-Hilk can you help me please I am a beginner c ++ I am a student in mechanics but I need qt for my end of study internship

                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #17

                                  @julie546 said in how i can identify and read the columns of a csv file ?:

                                  no member named toFloat in QStringList

                                  typo on my side, fixed it, but you should be able to fix that as well


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  0
                                  • J julie546

                                    @J-Hilk yes here is my output ("Y", "0.170138", "0.161089", "0.15901", "0.157898", "0.159706", "0.162481", "0.167204", "0.172876", "0.169802", "0.1677",.....................)
                                    theres an error with series-> append (coordXList.at(i).toFloat(), coordYList.toFloat());
                                    no member named toFloat in QStringList
                                    @KroMignon @J-Hilk can you help me please I am a beginner c ++ I am a student in mechanics but I need qt for my end of study internship

                                    KroMignonK Offline
                                    KroMignonK Offline
                                    KroMignon
                                    wrote on last edited by KroMignon
                                    #18

                                    @julie546 said in how i can identify and read the columns of a csv file ?:

                                    can you help me please I am a beginner c ++ I am a student in mechanics but I need qt for my end of study internship

                                    We have all started as beginner. I have studied electronics, not informatics, so I understand well your problem ;)

                                    The easiest way to create the QLineSeries, is to do it while parsing:

                                    QLineSeries * series = new QLineSeries();
                                    while (!filelidar.atEnd()) {
                                        QString line = filelidar.readLine();
                                        const auto parts = line.split (',');
                                        if(parts.size() >= 2)
                                        {
                                            bool ok;
                                            qreal x = parts.at(0).toFloat(&ok);
                                            if(!ok)
                                                continue;
                                            qreal y = parts.at(1).toFloat(&ok);
                                            if(!ok)
                                                continue;
                                           series->append(x, y);
                                        }
                                    }
                                    

                                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                    J 1 Reply Last reply
                                    4
                                    • KroMignonK KroMignon

                                      @julie546 said in how i can identify and read the columns of a csv file ?:

                                      can you help me please I am a beginner c ++ I am a student in mechanics but I need qt for my end of study internship

                                      We have all started as beginner. I have studied electronics, not informatics, so I understand well your problem ;)

                                      The easiest way to create the QLineSeries, is to do it while parsing:

                                      QLineSeries * series = new QLineSeries();
                                      while (!filelidar.atEnd()) {
                                          QString line = filelidar.readLine();
                                          const auto parts = line.split (',');
                                          if(parts.size() >= 2)
                                          {
                                              bool ok;
                                              qreal x = parts.at(0).toFloat(&ok);
                                              if(!ok)
                                                  continue;
                                              qreal y = parts.at(1).toFloat(&ok);
                                              if(!ok)
                                                  continue;
                                             series->append(x, y);
                                          }
                                      }
                                      
                                      J Offline
                                      J Offline
                                      julie546
                                      wrote on last edited by julie546
                                      #19

                                      @KroMignon yes it's really difficult to work in a field that you don't study
                                      thank you for your answer I tried like that but I still have an empty curve

                                      if (!filelidar.open(QFile::ReadOnly | QFile::Text) ) {
                                      qDebug() << "File not exists";
                                      } else {

                                                         QLineSeries * series = new QLineSeries();
                                                                 while (!filelidar.atEnd()) {
                                                                     QString line = filelidar.readLine();
                                                                     const auto parts = line.split (',');
                                                                     if(parts.size() >= 2)
                                                                     {
                                                                         bool ok;
                                                                         qreal x = parts.at(0).toFloat(&ok);
                                                                         if(!ok)
                                                                             continue;
                                                                         qreal y = parts.at(1).toFloat(&ok);
                                                                         if(!ok)
                                                                             continue;
                                                                        series->append(x, y);
                                                                     }
                                                                 }
                                      
                                      
                                      
                                                             QChart *chart = new QChart(); 
                                                             chart->legend()->hide();
                                                             chart->addSeries(series);
                                                             chart->createDefaultAxes();
                                                             chart->setTitle("Lidar tg15"); 
                                      
                                                             QChartView *chartView = new QChartView(chart);
                                                             chartView->setRenderHint(QPainter::Antialiasing);
                                      
                                                             QMainWindow window; 
                                                             window.setCentralWidget(chartView); 
                                                             window.resize(1000, 1000); 
                                                             window.show(); /
                                      

                                      }
                                      }

                                      Pl45m4P 1 Reply Last reply
                                      0
                                      • J julie546

                                        @KroMignon yes it's really difficult to work in a field that you don't study
                                        thank you for your answer I tried like that but I still have an empty curve

                                        if (!filelidar.open(QFile::ReadOnly | QFile::Text) ) {
                                        qDebug() << "File not exists";
                                        } else {

                                                           QLineSeries * series = new QLineSeries();
                                                                   while (!filelidar.atEnd()) {
                                                                       QString line = filelidar.readLine();
                                                                       const auto parts = line.split (',');
                                                                       if(parts.size() >= 2)
                                                                       {
                                                                           bool ok;
                                                                           qreal x = parts.at(0).toFloat(&ok);
                                                                           if(!ok)
                                                                               continue;
                                                                           qreal y = parts.at(1).toFloat(&ok);
                                                                           if(!ok)
                                                                               continue;
                                                                          series->append(x, y);
                                                                       }
                                                                   }
                                        
                                        
                                        
                                                               QChart *chart = new QChart(); 
                                                               chart->legend()->hide();
                                                               chart->addSeries(series);
                                                               chart->createDefaultAxes();
                                                               chart->setTitle("Lidar tg15"); 
                                        
                                                               QChartView *chartView = new QChartView(chart);
                                                               chartView->setRenderHint(QPainter::Antialiasing);
                                        
                                                               QMainWindow window; 
                                                               window.setCentralWidget(chartView); 
                                                               window.resize(1000, 1000); 
                                                               window.show(); /
                                        

                                        }
                                        }

                                        Pl45m4P Offline
                                        Pl45m4P Offline
                                        Pl45m4
                                        wrote on last edited by Pl45m4
                                        #20

                                        @julie546 said in how i can identify and read the columns of a csv file ?:

                                        qreal x = parts.at(0).toFloat(&ok);
                                        if(!ok)
                                        continue;
                                        qreal y = parts.at(1).toFloat(&ok);

                                        Print the values for x and y and check if these are the values from your file.
                                        The code from @KroMignon doesn't look wrong.


                                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                        ~E. W. Dijkstra

                                        J 1 Reply Last reply
                                        0
                                        • Pl45m4P Pl45m4

                                          @julie546 said in how i can identify and read the columns of a csv file ?:

                                          qreal x = parts.at(0).toFloat(&ok);
                                          if(!ok)
                                          continue;
                                          qreal y = parts.at(1).toFloat(&ok);

                                          Print the values for x and y and check if these are the values from your file.
                                          The code from @KroMignon doesn't look wrong.

                                          J Offline
                                          J Offline
                                          julie546
                                          wrote on last edited by julie546
                                          #21

                                          @Pl45m4 @KroMignon yes I have checked I have the right values ​​now I have a graph but it is not the same that I do with the csv file, I cannot find the same graph, what do you think please ?

                                          KroMignonK 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