How to write to csv file column by column?
-
Hello, I'm trying write to csv file some datas that I got from serial port. But when I export. All dahas shown on A column. For example I need Graph1 Datas should shown on A and graph 2 datas shown on B.
Here is code I tried to modify.
void MainWindow::exportArraysToCSV(QStringList labelList, QList < QList < double >> dataColums, QChar sep) { QString filters("CSV files (*.csv);;All files (*.*)"); QString defaultFilter("CSVi files (*.csv)"); QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(), filters, & defaultFilter); QFile file(fileName); if (file.open(QFile::WriteOnly | QFile::Append)) { QTextStream data( & file); QStringList strList; foreach(auto label, labelList) { if (label.length() > 0) strList.append("\"" + label + "\""); else strList.append(""); } data << strList.join(sep) << "\n"; int maxRowCount = 0; foreach(auto column, dataColums) maxRowCount = qMax(maxRowCount, column.count()); for (int i = 5; i < maxRowCount; ++i) // rows { strList.clear(); for (int j = 0; j < 2; ++j) // columns { if (i < dataColums[j].count()) strList.append(QString::number(dataColums[j][i], 'f')); else strList.append("\"\" "); } data << strList.join(sep) + "\n"; } file.close(); } }Here 's how look csv file When I export
How can I solve this issue?
-
Hello, I'm trying write to csv file some datas that I got from serial port. But when I export. All dahas shown on A column. For example I need Graph1 Datas should shown on A and graph 2 datas shown on B.
Here is code I tried to modify.
void MainWindow::exportArraysToCSV(QStringList labelList, QList < QList < double >> dataColums, QChar sep) { QString filters("CSV files (*.csv);;All files (*.*)"); QString defaultFilter("CSVi files (*.csv)"); QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(), filters, & defaultFilter); QFile file(fileName); if (file.open(QFile::WriteOnly | QFile::Append)) { QTextStream data( & file); QStringList strList; foreach(auto label, labelList) { if (label.length() > 0) strList.append("\"" + label + "\""); else strList.append(""); } data << strList.join(sep) << "\n"; int maxRowCount = 0; foreach(auto column, dataColums) maxRowCount = qMax(maxRowCount, column.count()); for (int i = 5; i < maxRowCount; ++i) // rows { strList.clear(); for (int j = 0; j < 2; ++j) // columns { if (i < dataColums[j].count()) strList.append(QString::number(dataColums[j][i], 'f')); else strList.append("\"\" "); } data << strList.join(sep) + "\n"; } file.close(); } }Here 's how look csv file When I export
How can I solve this issue?
@mangekoyu
Have you looked up the format of CSV files so that you know what you need to output? Where in your code are outputting the field separator to separate fields (columns) in each line? -
@JonB said in How to write to csv file column by column?:
Neyi çıktı almanız gerektiğini bilmek için CSV dosyalarının biçimine baktınız mı?
On the examples I watched, They did what I want but I couldnt make it on this code.
@mangekoyu
Please start by taking the time to look up what the format of a CSV file is, and how you separate columns in it, else how do you expect to write the required format?