目录
MiniCSV简介
之前写了一篇基于C语言字符串操作函数的csv文件解析:C语言解析csv格式文件,本文介绍一个开源简洁的csv解析库的使用:MiniCSV,使用标准C语言设计。
A tiny, fast, simple, single-file, BSD-licensed CSV parsing library in C.
Should be able to handle CSV oddities: multi-lines, escaped rows, escaped characters in escaped rows, empty rows, rows with a variable number of columns, Windows or Unix-style line endings.
Doesn't perform any heap allocations.
一个小型、快速、简单、单文件、BSD许可的C语言CSV解析库。
应该能够处理CSV的奇怪之处:多行、转义行、转义列中的转义字符、空行、列数可变的行、Windows或Unix风格的行结尾。
不执行任何堆分配。
开源地址
C语言版本
https://github.com/jedisct1/minicsv
C++版本
https://gitee.com/null_237_8629/minicsv_cpp
只有一个.c和一个.h文件,使用起来非常简单。
官方示例
#include <stdio.h> #include <stdlib.h> #include "stdint.h" #include "minicsv.h" int display_cols(char **cols, uint8_t cols_count) { printf("cols=%d: ", cols_count); for(int i = 0; i < cols_count; i++) printf("[%s]\t", cols[i]); printf("\n"); return 0; } int main(void) { char *cols[7]; //每行逗号的个数+1 char str_csv[] = "line1, aa, bb, cc, dd, ee, ff\nline2, 11, 22, 33, 44\nline3, 1,2,3,4,5"; char *r = str_csv; uint32_t cols_count; //size_t or uint32_t uint8_t cols_max = sizeof(cols) / sizeof(cols[0]); //最多解析多少列 /* line1, aa, bb, cc, dd, ee, ff line2, 11, 22, 33, 44 line3, 1,2,3,4,5 */ printf("csv string: \n%s\n\n", r); //解析第1行 while(*r != NULL) { r = minicsv_parse_line(r, cols, &cols_count, cols_max); //cols_count=当前行解析出的列数 display_cols(cols, cols_count); } printf("\nfinish!\n"); return 0; }
运行结果:
csv string:
line1, aa, bb, cc, dd, ee, ff
line2, 11, 22, 33, 44
line3, 1,2,3,4,5
cols=7: [line1] [ aa] [ bb] [ cc] [ dd] [ ee] [ ff]
cols=5: [line2] [ 11] [ 22] [ 33] [ 44]
cols=6: [line3] [ 1] [2] [3] [4] [5]
finish!
csv文件解析示例
csv文件内容:
序号,姓名,性别,年龄,职位,兼任,备注
1,张珊,女,29,产品经理
2,李思,男,31,架构师,兼产品副经理,试用
3,王伟,男,27,开发工程师
4,赵丽,女,27,测试工程师,,实习
minicsv解析示例:
#include "stdio.h" #include "stdlib.h" #include "stdint.h" #include "minicsv.h" #define CSV_PATH "./demo.csv" /* 序号,姓名,性别,年龄,职位,兼任,备注 1,张珊,女,29,产品经理 2,李思,男,31,架构师,兼产品副经理,试用 3,王伟,男,27,开发工程师 4,赵丽,女,27,测试工程师,,实习 */ int display_cols(char **cols, uint8_t cols_count); int main() { FILE *fp = fopen(CSV_PATH, "rw"); char buf[200]; char *cols[10]; uint8_t cols_max = sizeof(cols) / sizeof(cols[0]); uint32_t cols_count = 0; uint32_t line = 0; while(fgets(buf, sizeof(buf) / sizeof(buf[0]), fp) != NULL) { printf("line = %d, buf = %s", line, buf); minicsv_parse_line(buf, cols, &cols_count, cols_max); line++; display_cols(cols, cols_count); memset(buf, 0, sizeof(buf) / sizeof(buf[0])); } printf("finish! line count = %d\n", line); return 0; } int display_cols(char **cols, uint8_t cols_count) { printf("cols=%d ", cols_count); for(int i = 0; i < cols_count; i++) printf("[%s] ", cols[i]); printf("\n\n"); return 0; }
运行结果:
line = 0, buf = 序号,姓名,性别,年龄,职位,兼任,备注
cols=7 [序号] [姓名] [性别] [年龄] [职位] [兼任] [备注]
line = 1, buf = 1,张珊,女,29,产品经理
cols=5 [1] [张珊] [女] [29] [产品经理]
line = 2, buf = 2,李思,男,31,架构师,兼产品副经理,试用
cols=7 [2] [李思] [男] [31] [架构师] [兼产品副经理] [试用]
line = 3, buf = 3,王伟,男,27,开发工程师
cols=5 [3] [王伟] [男] [27] [开发工程师]
line = 4, buf = 4,赵丽,女,27,测试工程师,,实习
cols=7 [4] [赵丽] [女] [27] [测试工程师] [] [实习]
finish! line count = 5