通过Qt中的csv文件解析 [英] Parsing through a csv file in Qt

查看:999
本文介绍了通过Qt中的csv文件解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人熟悉如何解析通过csv文件并将其放在字符串列表中。现在我把整个csv文件和放入字符串列表。我试图找出是否有一种方法只获得第一列。

Is anyone familiar with how to parse through a csv file and put it inside a string list. Right now I am taking the entire csv file and putting into the string list. I am trying to figure out if there is a way to get only the first column.

#include "searchwindow.h"
#include <QtGui/QApplication>

#include <QApplication>
#include <QStringList>
#include <QLineEdit>
#include <QCompleter>
#include <QHBoxLayout>
#include <QWidget>
#include <QLabel>

#include <qfile.h>
#include <QTextStream>


int main(int argc, char *argv[])
              {


 QApplication a(argc, argv);

 QWidget *widget = new QWidget();
 QHBoxLayout *layout = new QHBoxLayout();

 QStringList wordList;

 QFile f("FlightParam.csv");
 if (f.open(QIODevice::ReadOnly))
      {
 //file opened successfully
QString data;
data = f.readAll();
wordList = data.split(',');

f.close();
 }


QLabel *label = new QLabel("Select");
QLineEdit *lineEdit = new QLineEdit;
label->setBuddy(lineEdit);

QCompleter *completer = new QCompleter(wordList);
completer->setCaseSensitivity(Qt::CaseInsensitive); //Make caseInsensitive selection


lineEdit->setCompleter(completer);

layout->addWidget(label);
layout->addWidget(lineEdit);

widget->setLayout(layout);
widget->showMaximized();


return a.exec();

}

推荐答案

到那里:

1,2,3,
4,5,6,
7,8,9,


b $ b

main.cpp



main.cpp

#include <QFile>
#include <QStringList>
#include <QDebug>

int main()
{
    QFile file("FlightParam.csv");
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << file.errorString();
        return 1;
    }

    QStringList wordList;
    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        wordList.append(line.split(',').first());
    }

    qDebug() << wordList;

    return 0;
}



main.pro



main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp



构建并运行



Build and Run

qmake && make && ./main



输出



Output

("1", "4", "7")

这篇关于通过Qt中的csv文件解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆