Fast CSV는 다트용 고성능 CSV 파싱 라이브러리로, 여러 플랫폼에서 CSV 파일을 효율적으로 읽고 쓰기 위해 설계되었습니다. 표준 CSV 파싱을 지원하며 뛰어난 속도를 제공하고, 대용량 파일 처리 시 과도한 메모리 사용 없이 사용할 수 있는 실험적인 이벤트 기반 스트리밍 기능도 포함되어 있습니다. 데이터 내보내기/가져오기, 분석 또는 설정 로딩과 같은 신뢰성 있고 빠른 CSV 처리가 필요한 애플리케이션에 적합합니다. 패키지는 가볍고 Flutter 프로젝트에 원활하게 통합됩니다.
사용 사례
CSV 파일에서 데이터 가져오기
CSV 형식으로 데이터 내보내기
대규모 CSV 데이터셋 처리
구성 파일 읽기
스프레드시트 유사 데이터 파싱
백엔드 데이터 소스와의 통합
주요 기능
고속 파싱
실험적 스트리밍 파서
크로스 플랫폼 지원
기본 사용을 위한 간단한 API
경량이고 효율적
UTF-8 인코딩을 지원합니다
적합한 대상
CSV 처리가 필요한 Flutter 앱
백엔드 서비스가 CSV 데이터 처리 중
데스크톱 및 웹 애플리케이션
낮은 메모리 사용으로 대용량 파일 처리
빠른 데이터 수집이 필요한 애플리케이션
성능을 우선시하는 개발자
고려 사항
스트리밍 기능은 실험적입니다
제한된 고급 형식 옵션
내장된 CSV 생성 기능 없음(해석만 가능)
매우 복잡한 CSV 구조에 최적화되지 않음
손으로 잘못된 입력을 처리해야 합니다
기본적으로 GUI 통합 없음
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
fast_csv
Classic CSV parsers suitable for most use cases. Pretty fast parsing. With experimental event-based streaming parsing.
Version: 0.2.11
About testing generated parsers
Both parsers have been tested in both operating modes, synchronous and asynchronous modes.
For testing, files from the following source are used: CSV is a data directory which contains examples of CSV files, a flat file format describing values in a table.
https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html
All files are parsed except for the malformed file mlb_players.csv.
It has unclosed quote on row 1036.
Example of the parser usage
An example of a simple way to parse 1000K rows (53M UTF-16 code units) in chunks (by ~0.53M UTF-16 code units) using events without consuming a lot of memory (during parsing) and simultaneously saving this parsed data into a virtual database (by 10K rows per transaction).
import'dart:async';
import'package:fast_csv/csv_converter.dart';
Future<void> main(List<String> args) async {
_exampleParseString();
await_exampleParseStreamWithEvents();
}
const _data = '''
Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00
''';
Stream<String> _createStream() {
// Create the stream with 1000000 rowsconst count = 1000 * 1000;
final controller = StreamController<String>();
final sink = controller.sink;
const row = '1999,Chevy,"Venture В«Extended EditionВ»","",4900.00';
const rowsInChunk = count ~/ 100;
final chunk = List.generate(rowsInChunk, (i) => row).join('\n');
print('Total data amount ${row.length * count} UTF-16 code units.');
print('The data will arrive in ${chunk.length} UTF-16 code unit chunks.');
var i = 0;
Timer.periodic(Duration.zero, (timer) {
sink.add(chunk);
i += rowsInChunk;
if (i < count) {
sink.add('\n');
}
if (i >= count) {
controller.close();
timer.cancel();
}
});
return controller.stream;
}
Future<void> _exampleParseStreamWithEvents() async {
print('=========================');
print('Start streaming parsing with events');
// Get external datafinal stream = _createStream();
final sw = Stopwatch();
sw.start();
final parser = _MyParser(() async {
print('Saving to virtual database complete in ${sw.elapsed}');
sw.stop();
});
await stream.transform(CsvConverter(parser: parser)).first;
}
void_exampleParseString() {
print('=========================');
print('Parsing string');
final result = CsvConverter().convert(_data);
print(result.join('\n'));
for (var i = 1; i < result.length; i++) {
final row = result[i];
final car = row[1];
final price = num.parse(row[4]);
print('$car$price');
}
}
class_MyParserextendsCsvParser{
finalFuture<void> Function()? onComplete;
int _count = 0;
int _totalCount = 0;
int _transactionCount = 0;
finalList<List<String>> _rows = [];
_MyParser([this.onComplete]);
@overridevoidbeginEvent(CsvParserEvent event) {
if (event == CsvParserEvent.startEvent) {
_count = 0;
_totalCount = 0;
_transactionCount = 0;
_rows.clear();
}
}
@override
R? endEvent<R>(CsvParserEvent event, R? result, bool ok) {
voidsaveRows(bool isLast) {
final rows = _rows.toList();
_rows.clear();
Timer.run(() async {
// Asynchronous saving to the database.await_saveToDatabase(rows, isLast);
});
}
if (ok) {
switch (event) {
caseCsvParserEvent.rowEvent:
final row = result asList<String>;
_rows.add(row);
if (_rows.length > 10000) {
saveRows(false);
}
// Free memory
result = const <String>[] as R;
break;
caseCsvParserEvent.startEvent:
saveRows(true);
if (onComplete != null) {
Timer.run(onComplete!);
}
// Completely freeing memory from the entire list
result = const <List<String>>[] as R;
default:
}
}
return result;
}
Future<void> _saveToDatabase(List<List<String>> rows, bool isLast) async {
_transactionCount++;
_count += rows.length;
_totalCount += rows.length;
if (_count > 100000 || isLast) {
print(
'Saved to virtual database $_totalCount row(s) in $_transactionCount transaction(s)');
_count = 0;
}
if (isLast) {
print(
'Totally saved to virtual database $_totalCount row(s) in $_transactionCount transaction(s)');
}
}
}
Output:
=========================
Parsing string
[Year, Make, Model, Description, Price]
[1997, Ford, E350, ac, abs, moon, 3000.00]
[1999, Chevy, Venture"Extended Edition", , 4900.00]
[1999, Chevy, Venture"Extended Edition, Very Large", , 5000.00]
[1996, Jeep, GrandCherokee, MUST SELL!
air, moon roof, loaded, 4799.00]
Ford3000.0Chevy4900.0Chevy5000.0Jeep4799.0
=========================
Start streaming parsing with events
Total data amount 52000000 UTF-16 code units.
The data will arrive in529999 UTF-16 code unit chunks.
Saved to virtual database 100010row(s) in10transaction(s)
Saved to virtual database 200020row(s) in20transaction(s)
Saved to virtual database 300030row(s) in30transaction(s)
Saved to virtual database 400040row(s) in40transaction(s)
Saved to virtual database 500050row(s) in50transaction(s)
Saved to virtual database 600060row(s) in60transaction(s)
Saved to virtual database 700070row(s) in70transaction(s)
Saved to virtual database 800080row(s) in80transaction(s)
Saved to virtual database 900090row(s) in90transaction(s)
Saved to virtual database 1000000row(s) in100transaction(s)
Totally saved to virtual database 1000000row(s) in100transaction(s)
Saving to virtual database complete in0:00:04.087358
An example of using a configurable parser
This parser is slightly slower than the non-configurable parser.
The difference between using a normal parser and using a configurable parser is that you can specify a field separator.
Any value (such as a space or semicolon).
import'package:fast_csv/csv_ex_converter.dart';
voidmain(List<String> args) {
final parser = CsvExParser(separator: ';');
final result = CsvExConverter(parser: parser).convert(_data);
print(result.join('\n'));
for (var i = 1; i < result.length; i++) {
final row = result[i];
final car = row[1];
final price = num.parse(row[4]);
print('$car$price');
}
}
const _data = '''
Year;Make;Model;Description;Price
1997;Ford;E350;"ac, abs, moon";3000.00
1999;Chevy;"Venture ""Extended Edition""";"";4900.00
1999;Chevy;"Venture ""Extended Edition, Very Large""";"";5000.00
1996;Jeep;Grand Cherokee;"MUST SELL!
air, moon roof, loaded";4799.00
''';
About the implementation of parsers
Parsers are generated from PEG grammars.
Software used to generate parsers
Below is the source code for one of the grammars.