[C++] Introduction and use of third-party open source csv parsing library

What is csv format?

CSV format (Comma-Separated Values) is a common spreadsheet file format that uses commas (or other specific characters, such as \t) as delimiters to separate different data fields. CSV files are usually used to store tabular data, such as personnel information, product information, etc.

CSV files have the following characteristics:

  1. Plain text format, easy to read and edit;
  2. The first line is usually the field name of each column, but there may be no column header;
  3. Each row represents a data record, and each column represents a data field;
  4. Supports sorting and filtering functions to facilitate data analysis and processing.

The extension of CSV files is usually ".csv", such as "data.csv".

Here are a few examples of comma-delimited CSV files:

1. Personnel information table (comma separated)

姓名,年龄,性别,地址
张三,25,男,北京
李四,30,女,上海
王五,28,男,广州

2. Product information table (\t separated)

商品编号    商品名称    单价    库存
001    电视    3000    50
002    冰箱    4000    30
003    洗衣机    2000    20

3. Student performance table (comma separated, no column header information)

001,张三,85,90,78
002,李四,78,85,92
003,王五,92,88,80

CSV files can be opened using a text editor (such as notepad++) or spreadsheet software (such as office excel), or imported into a corresponding application (such as python pandas frame) for data analysis and processing.

C++ open source csv parsing library csv-parse

rapidcsv

GitHub - auscanaoy/rapidcsv: C++ CSV parser library

 

Use Cases:

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++ code:

#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;
}

 For more examples, please refer to GitHub - auscanaoy/rapidcsv: C++ CSV parser library

Vince's CSV Parser

GitHub - vincentlaucsb/csv-parser: A modern C++ library for reading, writing, and analyzing CSV (and similar) files. 

 Use Cases:

#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;
}

 For more examples, please refer to: GitHub - vincentlaucsb/csv-parser: A modern C++ library for reading, writing, and analyzing CSV (and similar) files.

CSV Parser

GitHub - AriaFallah/csv-parser: Fast, header-only, extensively tested, C++11 CSV parser

CSVparser

GitHub - rsylvian/CSVparser: C++ parser for CSV file format

fast-cpp-csv-parser 

GitHub - ben-strasser/fast-cpp-csv-parser: fast-cpp-csv-parser

Guess you like

Origin blog.csdn.net/u011775793/article/details/135360872