Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. [Reading a csv file and removing the lines that I won't be needing]
Qt 6.11 is out! See what's new in the release blog

[Reading a csv file and removing the lines that I won't be needing]

Scheduled Pinned Locked Moved Solved Qt 6
5 Posts 2 Posters 1.1k 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.
  • A Offline
    A Offline
    appdev
    wrote on last edited by Christian Ehrlicher
    #1

    Hello guys,

    I hope you're doing very well.
    So I tried to read a csv file by displaying it in a QTableView.
    However there are some lines that I won't to delete so here is what I tried to do

    void MainWindow::on_actionOpen_triggered()
    {
        auto filename=QFileDialog::getOpenFileName(this,"Open", QDir::rootPath(), "CSV File(*.csv)");
        if (filename.isEmpty()) {
            return;
        }
        QFile file(filename);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            return;
        }
        QTextStream xin(&file);
        int ix = 0;
        while(!xin.atEnd()) {
            mModel->setRowCount(ix);
            auto line = xin.readLine();
            qDebug() << "line"<<line;
            auto values = line.split(";");
            qDebug() << "values" << values;
            const int colCount = values.size();
            mModel->setColumnCount(colCount);
    
            for (int jx = 0; jx< colCount; ++jx) {
               setValueAt(ix, jx, values.at(jx));
            }
           qDebug() << "values at jx" << values.at(3);
           if (values.at(3)!='5') { mModel->removeRow(ix);}
            ++ix;
        }
        file.close();
    }
    

    After displaying the whole csv file, I wanted to remove all the rows where the values of the third columns are different from '5' But that didn't work.

    Can someone please help me ?
    Thanks.

    JonBJ 1 Reply Last reply
    0
    • A appdev

      Hello guys,

      I hope you're doing very well.
      So I tried to read a csv file by displaying it in a QTableView.
      However there are some lines that I won't to delete so here is what I tried to do

      void MainWindow::on_actionOpen_triggered()
      {
          auto filename=QFileDialog::getOpenFileName(this,"Open", QDir::rootPath(), "CSV File(*.csv)");
          if (filename.isEmpty()) {
              return;
          }
          QFile file(filename);
          if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
              return;
          }
          QTextStream xin(&file);
          int ix = 0;
          while(!xin.atEnd()) {
              mModel->setRowCount(ix);
              auto line = xin.readLine();
              qDebug() << "line"<<line;
              auto values = line.split(";");
              qDebug() << "values" << values;
              const int colCount = values.size();
              mModel->setColumnCount(colCount);
      
              for (int jx = 0; jx< colCount; ++jx) {
                 setValueAt(ix, jx, values.at(jx));
              }
             qDebug() << "values at jx" << values.at(3);
             if (values.at(3)!='5') { mModel->removeRow(ix);}
              ++ix;
          }
          file.close();
      }
      

      After displaying the whole csv file, I wanted to remove all the rows where the values of the third columns are different from '5' But that didn't work.

      Can someone please help me ?
      Thanks.

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

      @appdev said in [Reading a csv file and removing the lines that I won't be needing]:

      But that didn't work.

      What didn't work? If you want to use removeRow() like you do

             if (values.at(3)!='5') { mModel->removeRow(ix);}
              ++ix;
      

      you will not want to increment ix if you remove the row, put it in an else?

      Or, make your decision about looking at column #3 before you commit to to adding the row in the first place?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        appdev
        wrote on last edited by appdev
        #3

        @JonB Thank your for the quick reply,

        What didn't work is that when i run my application and open the csv file, I get an empty file with 9 columns since my csv file contains 9 columns.

        Okay so for the first solution, you mean that I should do like this:

         if (values.at(3)!='5') { mModel->removeRow(ix);}
              else {  ++ix; }
        

        ?
        I tried it but it didn't work, it looks like the program isn't taking the if condition into consideration.
        I thought about the second solution you suggested but the row removal seems to be less complicated.

        JonBJ 1 Reply Last reply
        0
        • A appdev

          @JonB Thank your for the quick reply,

          What didn't work is that when i run my application and open the csv file, I get an empty file with 9 columns since my csv file contains 9 columns.

          Okay so for the first solution, you mean that I should do like this:

           if (values.at(3)!='5') { mModel->removeRow(ix);}
                else {  ++ix; }
          

          ?
          I tried it but it didn't work, it looks like the program isn't taking the if condition into consideration.
          I thought about the second solution you suggested but the row removal seems to be less complicated.

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

          @appdev
          I really don't know what your issue is.

           if (values.at(3)!='5') { mModel->removeRow(ix);}
                else {  ++ix; }
          

          Yes to the else.

          You seem to me to have a logic problem with your initialisation of int ix = 0; followed by mModel->setRowCount(ix);. So you set 0 rows and then call setValueAt(0, ...). You need to think about what you are doing, and at a guess mModel->setRowCount(ix + 1);.

          I thought about the second solution you suggested but the row removal seems to be less complicated.

          If you think so, fine. Otherwise put your columns into values, check whether values.at(3)!='5', and only add the row with its column values after the check on that column instead of before.

          1 Reply Last reply
          1
          • A Offline
            A Offline
            appdev
            wrote on last edited by
            #5

            @JonB Hello again, Thank you so much for your help.

            Well, I ended up using the second solution your suggested and which is making the decision about looking at column #3 before adding rows and it worked.

            Thank you again.

            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