Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to split a string with backslash and comma?



  • 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.


  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    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 ?



  • 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.


  • Lifetime Qt Champion

    @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
    


  • @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
    


  • @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.


  • Lifetime Qt Champion

    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



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

    Thank you .
    LeopoldI


Log in to reply