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. Read CSV and Parse, but Ignore Comma inside Double Quotes

Read CSV and Parse, but Ignore Comma inside Double Quotes

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.4k 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.
  • P Offline
    P Offline
    Phamy1289
    wrote on 25 Feb 2022, 14:58 last edited by Phamy1289
    #1

    I'm reading a CSV file where when it parses it reads the comma inside the double quotes. For example:

    The CSV: 1,"test\\test2\\Hello, World",4,0,2
    The output: 1
    "test\\test2\\Hello
    World
    4
    0
    2

    Expected output: 1
    "test\\test2\\Hello, World"
    4
    0
    2

    Also, when I use a regular expression it crashes.

    loadFile Function:

    string CsvFileImporter::loadFile(QString filename, vector<TrajectoryData *> & trajectoryList)
    {
      QFile csvFile(filename);
    
      string errorString = "";
      QStringList lines;
    
      if(csvFile.open(QIODevice::ReadOnly | QIODevice::Text))
      {
          QTextStream stream(&csvFile);
    
          while(!stream.atEnd())
          {
              QString line = stream.readLine();
    
              lines.append(line);
          }
    
    
          TrajectoryCSVImportDialog* dialog = new TrajectoryCSVImportDialog();
          dialog->setText(lines);
          bool importFlag = dialog->exec();
    
          if(importFlag)
          {
              int startLine = dialog->getLinesToSkip();
              QChar separator = dialog->getSeparator();
              double timeStep = dialog->getTimeStep();
    
    //          QStringList fieldNames = dialog->getFields();
              QList<int> fieldMetrics = dialog->getMetrics();
              QList<int> fieldColumns = dialog->getColumnNumber();
              QList<double> fieldFactors = dialog->getFactors();
    
              parseFile(lines, startLine,
                        separator,
                        timeStep,
                        fieldMetrics,
                        fieldColumns,
                        fieldFactors,
                        trajectoryList);
          }
          else
          {
              errorString = "Canceled by user";
              QApplication::restoreOverrideCursor();
          }
      }
    
      return errorString;
    

    parseFile Function:

    void CsvFileImporter::parseFile(QStringList lines,
                                    int startLine,
                                    QChar separator,
                                    double timeStep,
                                    QList<int> fieldMetrics,
                                    QList<int> fieldColumns,
                                    QList<double> fieldFactors,
                                    vector<TrajectoryData *> & trajectoryList)
    {
    //Some code
    
      for(int i=startLine;i<(int)lines.size();i++)
      {
          QStringList data = lines[i].split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
          //Some More Code
       }
    }
    
    J 1 Reply Last reply 25 Feb 2022, 15:43
    0
    • P Phamy1289
      25 Feb 2022, 14:58

      I'm reading a CSV file where when it parses it reads the comma inside the double quotes. For example:

      The CSV: 1,"test\\test2\\Hello, World",4,0,2
      The output: 1
      "test\\test2\\Hello
      World
      4
      0
      2

      Expected output: 1
      "test\\test2\\Hello, World"
      4
      0
      2

      Also, when I use a regular expression it crashes.

      loadFile Function:

      string CsvFileImporter::loadFile(QString filename, vector<TrajectoryData *> & trajectoryList)
      {
        QFile csvFile(filename);
      
        string errorString = "";
        QStringList lines;
      
        if(csvFile.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream stream(&csvFile);
      
            while(!stream.atEnd())
            {
                QString line = stream.readLine();
      
                lines.append(line);
            }
      
      
            TrajectoryCSVImportDialog* dialog = new TrajectoryCSVImportDialog();
            dialog->setText(lines);
            bool importFlag = dialog->exec();
      
            if(importFlag)
            {
                int startLine = dialog->getLinesToSkip();
                QChar separator = dialog->getSeparator();
                double timeStep = dialog->getTimeStep();
      
      //          QStringList fieldNames = dialog->getFields();
                QList<int> fieldMetrics = dialog->getMetrics();
                QList<int> fieldColumns = dialog->getColumnNumber();
                QList<double> fieldFactors = dialog->getFactors();
      
                parseFile(lines, startLine,
                          separator,
                          timeStep,
                          fieldMetrics,
                          fieldColumns,
                          fieldFactors,
                          trajectoryList);
            }
            else
            {
                errorString = "Canceled by user";
                QApplication::restoreOverrideCursor();
            }
        }
      
        return errorString;
      

      parseFile Function:

      void CsvFileImporter::parseFile(QStringList lines,
                                      int startLine,
                                      QChar separator,
                                      double timeStep,
                                      QList<int> fieldMetrics,
                                      QList<int> fieldColumns,
                                      QList<double> fieldFactors,
                                      vector<TrajectoryData *> & trajectoryList)
      {
      //Some code
      
        for(int i=startLine;i<(int)lines.size();i++)
        {
            QStringList data = lines[i].split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
            //Some More Code
         }
      }
      
      J Offline
      J Offline
      JonB
      wrote on 25 Feb 2022, 15:43 last edited by
      #2

      @Phamy1289
      Yes, you need to parse properly to deal correctly with commas inside quotes.

      I have not looked at them in detail, but what about:
      https://stackoverflow.com/a/38954889
      https://stackoverflow.com/a/30100233 (and the qtcsv library it references)
      ?

      P 1 Reply Last reply 25 Feb 2022, 17:25
      0
      • J JonB
        25 Feb 2022, 15:43

        @Phamy1289
        Yes, you need to parse properly to deal correctly with commas inside quotes.

        I have not looked at them in detail, but what about:
        https://stackoverflow.com/a/38954889
        https://stackoverflow.com/a/30100233 (and the qtcsv library it references)
        ?

        P Offline
        P Offline
        Phamy1289
        wrote on 25 Feb 2022, 17:25 last edited by
        #3

        @JonB Thank you so much for the links. Trying to use regular expressions was not working for me. Making another parse function based on the first link worked and was much easier to follow.

        J 1 Reply Last reply 25 Feb 2022, 18:08
        1
        • P Phamy1289
          25 Feb 2022, 17:25

          @JonB Thank you so much for the links. Trying to use regular expressions was not working for me. Making another parse function based on the first link worked and was much easier to follow.

          J Offline
          J Offline
          JonB
          wrote on 25 Feb 2022, 18:08 last edited by
          #4

          @Phamy1289
          Reg exs are great, but no substitute for parsing. When rules get too complex you are better writing parsing code than trying to find a robust reg ex.

          1 Reply Last reply
          1

          1/4

          25 Feb 2022, 14:58

          • Login

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