I want to save a set of data in csv file. I have no problem with registration. My problem is in the form of saving. All data are collected in column A. I have 2 graphs in total, graph1 and graph2. Graph1 should be in column A and Graph2 should be in column B. How can I set this up.
Here is code for export csv
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(",") << "\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(";") + "\n";
}
file.close();
}
}
I think this code for join all data on same column. How can I split it?
data << strList.join(",") << "\n";
Here is write log to csv function
void Logger::writeLogCSV(QStringList labelList, QList<double> dataList, bool addTime)
{
QTextStream out(logFile);
if (addTime)
{
if (csvLabelsBuffer.contains("time") == false)
csvLabelsBuffer.insert(0, "time");
}
if (labelList.count() == 0)
{
qDebug() << "Empty label list - abort csv write";
return;
}
bool canAddLabel = false;
for (auto i = 2; i < labelList.count(); ++i)
{
if (csvLabelsBuffer.count() == 2 || csvLabelsBuffer.contains(labelList[i]) == false)
{
canAddLabel = true;
csvLabelsBuffer.append(labelList[i]);
}
}
if (canAddLabel)
{
canAddLabel = false;
QStringList origFile = out.readAll().split(QRegExp("[\r\n]"), QString::SplitBehavior::SkipEmptyParts);
for (auto i = 2; i < csvLabelsBuffer.count(); ++i)
out << "\"" + csvLabelsBuffer[i] + "\t";
out << "";
// out << "\n";
if (origFile.length() > 0)
{
while (origFile.first().contains("\""))
origFile.removeFirst();
}
logFile->resize(0); // delete contents !
for (auto i = 2; i < origFile.count(); ++i) // Start from second line (data without first line which contains labels)
out << origFile[i] + ",";
return;
}
// add Data !
for (auto i = 2; i < csvLabelsBuffer.count(); ++i)
{
out.atEnd(); // ???
int index = labelList.indexOf(csvLabelsBuffer[i]);
if (index >= 0 && index < dataList.count())
out << QString::number(dataList[index], 'f') + ",";
else if (csvLabelsBuffer[i] == "time")
out << QDateTime::currentDateTimeUtc().toString("yyyy-MM-ddTHH:MM:ss.zzzZ") + ',';
}
out << "\n \n";
}
QList<QList<double>> dataColums({{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}});
your code prints 1-5 in the first column, and 6-10 in the second column (iterating from 0 of course, not from 5). – Izzo"Graph1, Graph2"
in the csv? Excel will honor the quotes and put it in a single column between double quotes. – TrilobitestrList.join(",")
is to collect all the data in one place. I'm wondering if this could be my problem and I don't know how to get rid of joinen. – Praetoriandata << strList.join(",") << "\n";
and see what the contents ofstrList
is. If at that line the strList contains only 1 item your bug is above this code. – Trilobite