CSV格式(Comma-Separated Values)是一种常见的电子表格文件格式,以逗号(或其它特定字符,比如\t)作为分隔符来分隔不同的数据字段。CSV文件通常用于存储表格数据,例如人员信息、商品信息等。
CSV文件具有以下特点:
CSV文件的扩展名通常是“.csv”,例如“data.csv”。
以下是几个用逗号分割的CSV文件的例子:
1. 人员信息表(逗号分隔)
姓名,年龄,性别,地址
张三,25,男,北京
李四,30,女,上海
王五,28,男,广州
2. 商品信息表(\t分隔)
商品编号 商品名称 单价 库存
001 电视 3000 50
002 冰箱 4000 30
003 洗衣机 2000 20
3. 学生成绩表(逗号分隔,没有列头信息)
001,张三,85,90,78
002,李四,78,85,92
003,王五,92,88,80
CSV文件可以使用文本编辑器(比如notepad++)或电子表格软件(比如office excel)打开,或者将其导入到相应的应用程序(比如python pandas frame)中进行数据分析和处理。
GitHub - auscanaoy/rapidcsv: C++ CSV parser library
?
使用案例:
colhdr.csv:
Open,High,Low,Close,Volume,Adj Close
64.529999,64.800003,64.139999,64.620003,21705200,64.620003
64.419998,64.730003,64.190002,64.620003,20235200,64.620003
64.330002,64.389999,64.050003,64.360001,19259700,64.360001
64.610001,64.949997,64.449997,64.489998,19384900,64.489998
64.470001,64.690002,64.300003,64.620003,21234600,64.620003
c++代码:
#include <iostream>
#include <vector>
#include "rapidcsv.h"
int main()
{
rapidcsv::Document doc("examples/colhdr.csv");
std::vector<float> col = doc.GetColumn<float>("Close");
std::cout << "Read " << col.size() << " values." << std::endl;
}
?更多例子,可以参考GitHub - auscanaoy/rapidcsv: C++ CSV parser library
?使用案例:
#include <csv.hpp>
struct GerberHoleInfo{
double xInch;
double yInch;
double rInch;
double xPixel;
double yPixel;
double rPixel;
double inch2PixelRatio = 360.0;
};
std::vector<GerberHoleInfo> parseGerberHoleInfos(const std::string& gerberFilePath, double inch2PixelRatio){
std::vector<GerberHoleInfo> holeInfos;
csv::CSVFormat format;
format.delimiter(',').quote(false).no_header();
csv::CSVReader reader(gerberFilePath, format);
for(csv::CSVRow& row:reader){
double x = std::stod(row[0].get());
double y = std::stod(row[1].get());
double r = std::stod(row[2].get());
GerberHoleInfo holeInfo;
holeInfo.xInch = x;
holeInfo.yInch = y;
holeInfo.rInch = r;
holeInfo.inch2PixelRatio = inch2PixelRatio;
holeInfo.xPixel = x * inch2PixelRatio;
holeInfo.yPixel = y * inch2PixelRatio;
holeInfo.rPixel = r * inch2PixelRatio;
holeInfos.push_back(holeInfo);
}
return holeInfos;
}
GitHub - AriaFallah/csv-parser: Fast, header-only, extensively tested, C++11 CSV parser
GitHub - rsylvian/CSVparser: C++ parser for CSV file format
GitHub - ben-strasser/fast-cpp-csv-parser: fast-cpp-csv-parser