当前位置 : 主页 > 编程语言 > java >

Qt 笔记4--Qt 读写CSV

来源:互联网 收集:自由互联 发布时间:2022-09-02
Qt 笔记4--Qt 读写CSV CSV(Comma-Separated Values),即逗号分隔值,有时也称为字符分隔值,因为分隔字符也可以不是逗号,其文件以纯文本形式存储表格数据;CSV是一种通用的、相对简单的


Qt 笔记4--Qt 读写CSV

 

CSV(Comma-Separated Values),即逗号分隔值,有时也称为字符分隔值,因为分隔字符也可以不是逗号,其文件以纯文本形式存储表格数据;CSV是一种通用的、相对简单的文件格式,被用户、商业和科学广泛应用。Qt中似乎没有直接处理CSV的类,因此需要按照CSV的格式进行解析和生成,以下为笔者根据需要写的一个CSV读写案例,后续有相关功能函数也会在此处加以补充。

 

1、源代码

CSV不同行之间使用 \n 符隔开,行内各列之间使用 , 隔开,根据该原则:读取的时候,只需读取所有文本,逐行读出,每一行去除  \n 和 , 即可;生成的时候,将各字段依次放到字符串中,并用 , 隔开,然后在末尾添加 \n 换行符,最后将字符串依次以附加的形式写到文本中即可。
该案例源代码包括三个主要文件:csvclass.h、csvclass.cpp、main.cpp,源码如下:

csvclass.h

#ifndef CSVCLASS_H
#define CSVCLASS_H

#include <QDebug>
#include <QFile>
class CsvClass
{
public:
CsvClass();
QList<QStringList> ReadFromCSV(QString PathName);
void SaveToCSV(QList<QStringList> CsvList,QString PathName);
};

#endif // CSVCLASS_H

csvclass.cpp

#include "csvclass.h"

CsvClass::CsvClass()
{
qDebug()<<"This is CSV exapmle!\n";
}

QList<QStringList> CsvClass::ReadFromCSV(QString PathName)
{
QList<QStringList> retList;
qDebug().noquote()<<"Original CSV name:"<<PathName;
QFile file(PathName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return retList;
while (!file.atEnd()) {
//QByteArray line = file.readLine();
QString line = file.readLine();
line.replace('\n',"");//delete \n symbol
QStringList lineList = line.split(',');
retList.append(lineList);
qDebug().noquote()<<lineList;
}
file.close();
return retList;
}

void CsvClass::SaveToCSV(QList<QStringList> CsvList,QString PathName)
{
qDebug().noquote()<<"\nDestination CSV name:"<<PathName;
for(int i=0;i<CsvList.size();i++)
{
QString tmpStr;
QStringList strList = CsvList.at(i);
for(int j=0;j<strList.size();j++){
tmpStr.append(strList.at(j));
if(j<(strList.size()-1)){
tmpStr.append(',');
}else{
tmpStr.append('\n');
}
}
qDebug().noquote()<<i<<'\t'<<tmpStr;
QFile file(PathName);
if(!file.open(QIODevice::WriteOnly| QIODevice::Text | QIODevice::Append)){
qDebug()<<"Open "<<PathName<<" Error!";
return;
}else{
QTextStream in(&file);
in<<tmpStr;
file.close();
}
}
}

main.cpp

#include <QCoreApplication>
#include <csvclass.h>

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

CsvClass *CsvObj = new CsvClass();
QList<QStringList> retList = CsvObj->ReadFromCSV("Data.csv");
CsvObj->SaveToCSV(retList,"Data_w.csv");
delete CsvObj;
return a.exec();
}

2、测试结果

以下为此时CSV原始内容:

ymd,power,irradiation,bWendu,yWendu,bTianqi,yTianqi,fengli,aqi,aqiInfo,aqiLevel,sunny_percent,rain_percent,rain_full,pressure_avg,hum_avg,tem_max,tem_min,tem_avg,wspd_avg
20160401,-1,,27,19,阴,多云,3,77,良,,65,0,0,1006.91,78.91,26.9,20.6,22.87,1.08
20160402,-1,,26,20,阴,,3,66,良,,25,29,0,1007.96,83.79,25.8,20.4,22.42,1.34
20160403,-1,,26,19,雷阵雨,,3,51,良,,0,83,0,1007,81.92,27.8,20.7,23.59,1.39
20160404,-1,,25,21,阵雨,,3,38,优,,33,66,22.8,1004.96,85.92,28,20.2,23.44,1.35
20160405,-1,,27,20,多云,,3,34,优,,100,0,0,1005.79,87.42,26,20,22.95,1.4
20160406,-1,,27,20,阴,,3,29,优,,50,33,0,1005.62,79.29,29.8,21.4,24.68,1.56
20160407,-1,,29,22,多云,,3,34,优,,58,41,0,1005.62,82.58,28.8,22.6,25.18,1.49
20160408,-1,,27,21,小雨,,3,39,优,,83,16,0,1005.75,83,29.3,22.8,25.57,1.73
20160409,-1,,28,21,小雨,,3,37,优,,25,75,0.1,1004.12,87.79,27.1,23.5,25.08,1.82
20160410,-1,,26,21,中雨,小到中雨,4,35,优,,0,100,11,1001.75,91.58,24.6,21.2,22.96,1.41

测试输出结果如下:

This is CSV exapmle!

Original CSV name: Data.csv
(ymd, power, irradiation, bWendu, yWendu, bTianqi, yTianqi, fengli, aqi, aqiInfo, aqiLevel, sunny_percent, rain_percent, rain_full, pressure_avg, hum_avg, tem_max, tem_min, tem_avg, wspd_avg)
(20160401, -1, , 27, 19, 阴, 多云, 3, 77, 良, , 65, 0, 0, 1006.91, 78.91, 26.9, 20.6, 22.87, 1.08)
(20160402, -1, , 26, 20, 阴, , 3, 66, 良, , 25, 29, 0, 1007.96, 83.79, 25.8, 20.4, 22.42, 1.34)
(20160403, -1, , 26, 19, 雷阵雨, , 3, 51, 良, , 0, 83, 0, 1007, 81.92, 27.8, 20.7, 23.59, 1.39)
(20160404, -1, , 25, 21, 阵雨, , 3, 38, 优, , 33, 66, 22.8, 1004.96, 85.92, 28, 20.2, 23.44, 1.35)
(20160405, -1, , 27, 20, 多云, , 3, 34, 优, , 100, 0, 0, 1005.79, 87.42, 26, 20, 22.95, 1.4)
(20160406, -1, , 27, 20, 阴, , 3, 29, 优, , 50, 33, 0, 1005.62, 79.29, 29.8, 21.4, 24.68, 1.56)
(20160407, -1, , 29, 22, 多云, , 3, 34, 优, , 58, 41, 0, 1005.62, 82.58, 28.8, 22.6, 25.18, 1.49)
(20160408, -1, , 27, 21, 小雨, , 3, 39, 优, , 83, 16, 0, 1005.75, 83, 29.3, 22.8, 25.57, 1.73)
(20160409, -1, , 28, 21, 小雨, , 3, 37, 优, , 25, 75, 0.1, 1004.12, 87.79, 27.1, 23.5, 25.08, 1.82)
(20160410, -1, , 26, 21, 中雨, 小到中雨, 4, 35, 优, , 0, 100, 11, 1001.75, 91.58, 24.6, 21.2, 22.96, 1.41)

Destination CSV name: Data_w.csv
0 ymd,power,irradiation,bWendu,yWendu,bTianqi,yTianqi,fengli,aqi,aqiInfo,aqiLevel,sunny_percent,rain_percent,rain_full,pressure_avg,hum_avg,tem_max,tem_min,tem_avg,wspd_avg

1 20160401,-1,,27,19,阴,多云,3,77,良,,65,0,0,1006.91,78.91,26.9,20.6,22.87,1.08

2 20160402,-1,,26,20,阴,,3,66,良,,25,29,0,1007.96,83.79,25.8,20.4,22.42,1.34

3 20160403,-1,,26,19,雷阵雨,,3,51,良,,0,83,0,1007,81.92,27.8,20.7,23.59,1.39

4 20160404,-1,,25,21,阵雨,,3,38,优,,33,66,22.8,1004.96,85.92,28,20.2,23.44,1.35

5 20160405,-1,,27,20,多云,,3,34,优,,100,0,0,1005.79,87.42,26,20,22.95,1.4

6 20160406,-1,,27,20,阴,,3,29,优,,50,33,0,1005.62,79.29,29.8,21.4,24.68,1.56

7 20160407,-1,,29,22,多云,,3,34,优,,58,41,0,1005.62,82.58,28.8,22.6,25.18,1.49

8 20160408,-1,,27,21,小雨,,3,39,优,,83,16,0,1005.75,83,29.3,22.8,25.57,1.73

9 20160409,-1,,28,21,小雨,,3,37,优,,25,75,0.1,1004.12,87.79,27.1,23.5,25.08,1.82

10 20160410,-1,,26,21,中雨,小到中雨,4,35,优,,0,100,11,1001.75,91.58,24.6,21.2,22.96,1.41

3、说明

以上代码默认测试环境为Qt 5.7,测试系统为Windows7 x64

上一篇:Java 基本数据类型
下一篇:没有了
网友评论