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();
}
There you go:
FlightParam.csv
main.cpp
main.pro
Build and Run
Output
Here is the code I usually use. I'm the author, consider this as-is, public domain. It has a similar feature-set and concept as CodeLurker's code except the state machine is represented differently, the code is a bit shorter.
in
, aQTextStream
.row
, aQStringList
that will receive the row.true
if a row was read,false
if EOF.std::runtime_error
if an error occurs.It parses Excel style CSV's, handling quotes and double-quotes appropriately, and allows newlines in fields. Handles Windows and Unix line endings properly as long as your file is opened with
QFile::Text
. I don't think Qt supports old-school Mac line endings, and this doesn't support binary-mode untranslated line-endings, but for the most part this shouldn't be a problem these days.Other notes:
x"y"z
asxyz
, wasn't sure what the rule for mid-string quotes was. I have no idea if this is correct.QChar
should be trivial.Example:
then
I am not sure add function exists or not to add to an array - let call column 1
Try qtcsv library for reading and writing csv-files. Example:
I tried to make it small and easy-to-use. See Readme file for library documentation and other code examples.
One might prefer to do it this way:
Edit: I've finally got around to getting this to trim spaces before and after the fields. No whitespace nor commas are trimmed inside quotes. Otherwise, all whitespace is trimmed from the start and end of a field. After puzzling about this for a while, I hit on the idea that the quotes could be left around the field; and so all fields could be trimmed. That way, only whitespace before and after quotes is removed. A final step was then added, to strip out quotes for fields that start and end with quotes.
Here is a more or less challenging test case:
This corresponds to the file
where <TAB> represents the tab character; and each line is fed into parseCSV() in turn.
Its output is (where qDebug() is representing quotes in the string with
\"
and putting things in quotes and parens):You can observe that the quote and the extra spaces were preserved inside the quote for item "two". In the malformed case for "and a half", the space before the quote, and those after the last word, were removed; but the others were not. Missing terminal spaces in this routine could be an indication of a missing terminal quote. Quotes in a field that don't start or end it are just treated as part of a string. A quote isn't removed from the end of a field if one doesn't start it. To detect an error here, just check for a field that starts with a quote, but doesn't end with one; and/or one that contains quotes but doesn't start and end with one, in the final loop.
More than was needed for yer test case, I know; but a solid general answer to the ?, nonetheless - perhaps for others who have found it.
Adapted from: https://github.com/hnaohiro/qt-csv/blob/master/csv.cpp
What you are looking for is a QTextStream class. It provides all kind of interfaces for reading and writing files.
A simple example:
Alternatively yes, you can do something like this which would have the same result: