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 write to csv file column by column?

How to write to csv file column by column?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 700 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.
  • mangekoyuM Offline
    mangekoyuM Offline
    mangekoyu
    wrote on last edited by
    #1

    Hello, I'm trying write to csv file some datas that I got from serial port. But when I export. All dahas shown on A column. For example I need Graph1 Datas should shown on A and graph 2 datas shown on B.

    Here is code I tried to modify.

    void MainWindow::exportArraysToCSV(QStringList labelList, QList < QList < double >> dataColums, QChar sep) {
      QString filters("CSV files (*.csv);;All files (*.*)");
      QString defaultFilter("CSVi files (*.csv)");
      QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
        filters, & defaultFilter);
      QFile file(fileName);
    
      if (file.open(QFile::WriteOnly | QFile::Append)) {
        QTextStream data( & file);
        QStringList strList;
    
        foreach(auto label, labelList) {
          if (label.length() > 0)
            strList.append("\"" + label + "\"");
          else
            strList.append("");
        }
    
        data << strList.join(sep) << "\n";
    
        int maxRowCount = 0;
        foreach(auto column, dataColums)
        maxRowCount = qMax(maxRowCount, column.count());
    
        for (int i = 5; i < maxRowCount; ++i) // rows
        {
          strList.clear();
          for (int j = 0; j < 2; ++j) // columns
          {
            if (i < dataColums[j].count())
              strList.append(QString::number(dataColums[j][i], 'f'));
    
    
            else
              strList.append("\"\" ");
          }
    
          data << strList.join(sep) + "\n";
    
        }
        file.close();
      }
    }
    

    Here 's how look csv file When I export

    How can I solve this issue?

    JonBJ 1 Reply Last reply
    0
    • mangekoyuM mangekoyu

      Hello, I'm trying write to csv file some datas that I got from serial port. But when I export. All dahas shown on A column. For example I need Graph1 Datas should shown on A and graph 2 datas shown on B.

      Here is code I tried to modify.

      void MainWindow::exportArraysToCSV(QStringList labelList, QList < QList < double >> dataColums, QChar sep) {
        QString filters("CSV files (*.csv);;All files (*.*)");
        QString defaultFilter("CSVi files (*.csv)");
        QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
          filters, & defaultFilter);
        QFile file(fileName);
      
        if (file.open(QFile::WriteOnly | QFile::Append)) {
          QTextStream data( & file);
          QStringList strList;
      
          foreach(auto label, labelList) {
            if (label.length() > 0)
              strList.append("\"" + label + "\"");
            else
              strList.append("");
          }
      
          data << strList.join(sep) << "\n";
      
          int maxRowCount = 0;
          foreach(auto column, dataColums)
          maxRowCount = qMax(maxRowCount, column.count());
      
          for (int i = 5; i < maxRowCount; ++i) // rows
          {
            strList.clear();
            for (int j = 0; j < 2; ++j) // columns
            {
              if (i < dataColums[j].count())
                strList.append(QString::number(dataColums[j][i], 'f'));
      
      
              else
                strList.append("\"\" ");
            }
      
            data << strList.join(sep) + "\n";
      
          }
          file.close();
        }
      }
      

      Here 's how look csv file When I export

      How can I solve this issue?

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

      @mangekoyu
      Have you looked up the format of CSV files so that you know what you need to output? Where in your code are outputting the field separator to separate fields (columns) in each line?

      1 Reply Last reply
      1
      • mangekoyuM Offline
        mangekoyuM Offline
        mangekoyu
        wrote on last edited by
        #3

        @JonB said in How to write to csv file column by column?:

        Neyi çıktı almanız gerektiğini bilmek için CSV dosyalarının biçimine baktınız mı?

        On the examples I watched, They did what I want but I couldnt make it on this code.

        JonBJ 1 Reply Last reply
        0
        • mangekoyuM mangekoyu

          @JonB said in How to write to csv file column by column?:

          Neyi çıktı almanız gerektiğini bilmek için CSV dosyalarının biçimine baktınız mı?

          On the examples I watched, They did what I want but I couldnt make it on this code.

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

          @mangekoyu
          Please start by taking the time to look up what the format of a CSV file is, and how you separate columns in it, else how do you expect to write the required format?

          1 Reply Last reply
          1

          • Login

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