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 string with backslash and comma?
Forum Updated to NodeBB v4.3 + New Features

How to split a string with backslash and comma?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 2.0k Views 1 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.
  • L Offline
    L Offline
    Leopold
    wrote on last edited by
    #1

    Hello,
    i searched the help for QRegexp but didn`t understand completely. I want to split a string like that
    : ""timestamp","rpm","coolant_temp_celcius","intake_air_temp_celcius","map_sensor_kpa","battery_voltage"

    The fields should keep the raw expression. : timestamp rpm coolant_temp_celsius and so on.
    I tried:
    QStringList fields = line.split(QRegExp("\\,\\"), QString::SkipEmptyParts)
    QStringList fields = line.split("(\",\")"); and a lot of other variations.
    Thank you for help.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #7

      Hi
      ahh its a CSV file and the first line is headers.
      Yeah then the QRegularExpression is not useful.

      alt text

      The reason .toDouble() fails is that is has " around its values
      so you should remove them

      QString item= fields.at(1);
      item = item.mid(1, item.length() -2 );
      y1.append(item.toDouble());
      

      ps you can use QStringRef also for a speed boost if you got many lines. ( thousands)
      https://doc.qt.io/qt-5/qstringref.html

      L 1 Reply Last reply
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        forum eats stuff so please use code ticks

        "\"timestamp\",\"rpm\",\"coolant_temp_celcius\",\"intake_air_temp_celcius\",\"map_sensor_kpa\",\"battery_voltage\"
        

        Are you sure actual data contains the backslash ?
        and not just from using qDebug() to write it out ?

        1 Reply Last reply
        1
        • L Offline
          L Offline
          Leopold
          wrote on last edited by
          #3

          and not just from using qDebug() to write it out ?
          Yes I think so, because it handles the complete string as one field.
          I have this: QStringList fields = line.split( ","); on other strings and there it works.
          But I will check with qDebug on the working strings.

          mrjjM 1 Reply Last reply
          1
          • L Leopold

            and not just from using qDebug() to write it out ?
            Yes I think so, because it handles the complete string as one field.
            I have this: QStringList fields = line.split( ","); on other strings and there it works.
            But I will check with qDebug on the working strings.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Leopold
            Hi
            What about just taking the actual text ?

             QString input=R"("\"timestamp\",\"rpm\",\"coolant_temp_celcius\",\"intake_air_temp_celcius\",\"map_sensor_kpa\",\"battery_voltage\")";
                QRegularExpression re("(\\w+)");
                QRegularExpressionMatchIterator i = re.globalMatch(input);
                while (i.hasNext()) {
                    QRegularExpressionMatch match = i.next();
                    QString word = match.captured(1);
                    qDebug().noquote() << word;
                }
            

            result

            timestamp
            rpm
            coolant_temp_celcius
            intake_air_temp_celcius
            map_sensor_kpa
            battery_voltage
            
            1 Reply Last reply
            2
            • L Offline
              L Offline
              Leopold
              wrote on last edited by
              #5

              @mrjj said in How to split a string with backslash and comma?:
              Hi mrjj,
              that causes me much more problems.
              I am reading from a csv file and the next lines are digids.```
              code_text"timestamp","rpm","coolant_temp_celcius","intake_air_temp_celcius","map_sensor_kpa","battery_voltage","throttle_pot_voltage","throttle_pot_percent","idle_switch","park_or_neutral_switch","fault_data_d","coolant_temp_sensor_fault","inlet_air_temp_sensor_fault","fault_data_e","fuel_pump_circuit_fault","throttle_pot_circuit_fault","idle_speed_deviation","ignition_advance","air_fuel_ratio","raw_lambda","lambda_mv","lambda_v","lambda_sensor_frequency","lambda_sensor_duty_cycle","lambda_sensor_status","closed_loop","long_term_trim","short_term_trim_percent","carbon_can_purge_valve_duty_cycle","idle_base_position","idle_error","throttle_angle","coil_time_microseconds","iac_position","ambient_temp","fuel_temp","turbo_boost_fault","ambient_temp_fault","fuel_temp_fault","knock_detected","temp_gauge_fault","air_con_clutch_fault","purge_valve_fault","map_sensor_fault","boost_valve_fault","idle_setting"
              "Mon Jul 20 08:35:15 GMT+01:00 2020","0","92","57","101","13.0","0.86","17.2","false","true","32","false","false","0","false","false","512","14.5","135","0","0.0","0.0","5","46","1","false","124","100","0","0","0","13","1394.0","35","23.0","85.0","false","false","true","false","false","false","false","false","false","0.0"
              "Mon Jul 20 08:35:15 GMT+01:00 2020","0","92","57","101","13.0","0.86","17.2","false","true","32","false","false","0","false","false","512","14.5","135","0","0.0","0.0","5","46","1","false","124","100","0","0","0","13","1394.0","35","23.0","85.0","false","false","true","false","false","false","false","false","false","0.0"
              "Mon Jul 20 08:35:15 GMT+01:00 2020","0","92","57","101","13.0","0.86","17.2","false","true","32","false","false","0","false","false","512","14.5","135","0","0.0","0.0","5","46","1","false","124","100","0","0","0","13","1394.0","35","23.0","85.0","false","false","true","false","false","false","false","false","false","0.0"

              If I read with QStringList fields = line.split( ","); from a simular txt file i have no problems.But when I read from the csv file the for y in my code will not work.

              code_text   QString line = in.readLine();
                                        while(!in.atEnd())
                                        {
                                            QString line = in.readLine();
                                            qDebug() << "Zeile1563:"<<line;
                                            qDebug() << "Zeile1564:"<<line_count;
                                            QStringList fields = line.split(',');
                                            if (line_count > 5)
                                            {
                                                //  x.append(QTime::fromString(fields.at(0),"hh:mm:ss").msecsSinceStartOfDay()/10000.0);//time
                                                // take first value and stored into x
                                                x.append(tick);
                                                tick++;
                                                y1.append(fields.at(1).toDouble()); // rpm
                                                y55.append(fields.at(1).toDouble());//rpm<1500
              
              1 Reply Last reply
              0
              • L Offline
                L Offline
                Leopold
                wrote on last edited by
                #6

                @Leopold said in How to split a string with backslash and comma?:

                If I read with QStringList fields = line.split( ","); from a simular txt file i have no problems.But when I read from the csv file the "append" for y in my code will not work.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #7

                  Hi
                  ahh its a CSV file and the first line is headers.
                  Yeah then the QRegularExpression is not useful.

                  alt text

                  The reason .toDouble() fails is that is has " around its values
                  so you should remove them

                  QString item= fields.at(1);
                  item = item.mid(1, item.length() -2 );
                  y1.append(item.toDouble());
                  

                  ps you can use QStringRef also for a speed boost if you got many lines. ( thousands)
                  https://doc.qt.io/qt-5/qstringref.html

                  L 1 Reply Last reply
                  4
                  • mrjjM mrjj

                    Hi
                    ahh its a CSV file and the first line is headers.
                    Yeah then the QRegularExpression is not useful.

                    alt text

                    The reason .toDouble() fails is that is has " around its values
                    so you should remove them

                    QString item= fields.at(1);
                    item = item.mid(1, item.length() -2 );
                    y1.append(item.toDouble());
                    

                    ps you can use QStringRef also for a speed boost if you got many lines. ( thousands)
                    https://doc.qt.io/qt-5/qstringref.html

                    L Offline
                    L Offline
                    Leopold
                    wrote on last edited by
                    #8

                    Hi mrjj
                    that was the solution.I would have searched for another few hours or days.

                    Thank you .
                    LeopoldI

                    1 Reply Last reply
                    2

                    • Login

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