如何解析CSV文件的示例代码

Author: Kagula

Env: VS2013Update4 QT5.4


#ifndef _ProductInfoTableStruct_H_
#define _ProductInfoTableStruct_H_

#pragma once

#include <QtCore>

#include <map>
#include <vector>

namespace kagula
{
	struct ProdInfoTableCSVStruct
	{
		void SetData(QString fileName);
	
		QString _version;
		std::vector<QString> _title;
		std::vector<QString> _titleDesp;
		std::vector<std::vector<QString>> _body;
	};
}


#endif

#include "ProdInfoTableCSVStruct.h"

namespace kagula
{
	void ProdInfoTableCSVStruct::SetData(QString fileName)
	{
		//read data from readonly file
		QString data;
		QStringList rowOfData;
		QStringList colOfData;

		qDebug() << "CSV filename is " << fileName;

		rowOfData.clear();
		colOfData.clear();

		QFile file(fileName);

		if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
		{
			return;
		}

		QTextStream in(&file);
		in.setCodec("GBK");
		while (!in.atEnd())
		{
			//Get line
			QString line = in.readLine();
			line = line + "\n";
			data = data + line;
		}
		file.close();
		//get data from local file end!

		//extract rowOfData.begin
		int doubleQuote = 0;
		for (long long i = 0; i < data.size(); i++)
		{
			if (data.at(i) == "\"")
			{
				doubleQuote++;
				continue;
			}
			if (data.at(i) == "\n" && doubleQuote % 2 == 0)
			{
				QString line = data.mid(0, i);
				data.remove(0, i);
				rowOfData.push_back(line);
				i = 0;
			}
		}
		//comment below, because linefeed symbol may be between a pair of double quotes.
		//rowOfData = data.split("\n");
		//extract rowOfData.end

		//how many records
		if (rowOfData.size() > 3)
		{
			_body.resize(rowOfData.size() - 3);
		}

		//parse body.
		for (int rowIndex = 0; rowIndex < rowOfData.size(); rowIndex++)
		{
			//extract to colOfData.begin
			{
				colOfData.clear();
				int doubleQuote = 0;
				//colOfData = rowOfData.at(rowIndex).split(";");

				QString data = rowOfData.at(rowIndex);
				for (int i = 0; i < data.size(); i++)
				{
					if (data.at(i) == "\"")
					{
						doubleQuote++;
						continue;
					}
					if ( (data.at(i) == "\t" || data.at(i)==",") && doubleQuote % 2 == 0)
					{
						QString col = data.mid(0, i);
						data.remove(0, i);
						col = col.trimmed();
						if (col.length() > 1 && col[0] == ',')
						{
							col.remove(0, 1);
						}
						colOfData.push_back(col);
						i = 0;
					}
				}
			}
			//extract to colOfData.end

			//get column number
			if (rowIndex >= 3)
			{
				_body[rowIndex - 3].resize(colOfData.size());
			}

			//
			for (int colIndex = 0; colIndex < colOfData.size(); colIndex++)
			{
				if (rowIndex == 0)
				{
					//first line, read version info.
					if (colIndex == 0)
					{
						_version = colOfData[colIndex];
					}
				}
				else if (rowIndex == 1)
				{
					//second line, read field name.
					QString fieldName = colOfData[colIndex];
					fieldName = fieldName.trimmed();
					_title.push_back(fieldName);
				}
				else if (rowIndex == 2)
				{
					//third line, read field name's description.
					QString fieldV = colOfData[colIndex];
					_titleDesp.push_back(fieldV);
				}
				else if (rowIndex >= 3)
				{
					//if more than 3, read data body.
					//remove quote symbol at head and tail if it's exist.
					QString fieldV = colOfData[colIndex];
					if (fieldV.size() > 1)
					{
						if (fieldV.at(0) == "\"")
						{
							fieldV.remove(0, 1);
						}
						if (fieldV.at(fieldV.size() - 1) == "\"")
						{
							fieldV.remove(fieldV.size() - 1, 1);
						}
					}
					_body[rowIndex - 3][colIndex] = fieldV;
				}//end else if
			}//end for
		}//end for
	}//end func
}//end namespace





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值