Fast CSV reader writer in c#.
Features
- Fully CSV standard compliant
- Multi-line
- Quoted columns
- Keeps spaces between delimiters
- Really fast reading and writing of CSV files (see performance)
- Tiny 8kb DLL compiled to
net40
or netstandard20
- Ability to get a typed list of objects from a CSV file
- Ability to filter a CSV file while loading
- Ability to specify a custom delimiter
Usage
4 // you can use fields or properties
8 public string Description;
12 // listcars = List<cars>
13 var listcars = fastCSV.ReadFile<cars>(
14 "csvstandard.csv", // filename
17 (o, c) => // to object function o : cars object, c : columns array read
28 fastCSV.WriteFile<LocalWeatherData>(
29 "filename2.csv", // filename
30 new string[] { "WBAN", "Date", "SkyCondition" }, // headers
32 list, // list of LocalWeatherData to save
33 (o, c) => // from object function
36 c.Add(o.Date.ToString("yyyyMMdd"));
37 c.Add(o.SkyCondition);
Helper functions for performance
fastCSV
has the following helper functions:
int ToInt(string s)
creates an int
from a string
int ToInt(string s, int index, int count)
creates an int
from a substring
DateTime ToDateTimeISO(string value, bool UseUTCDateTime)
creates an ISO standard DateTime
i.e. yyyy-MM-ddTHH:mm:ss
( optional part<tt>.nnnZ)
2 public class LocalWeatherData
6 public string SkyCondition;
9 var list = fastCSV.ReadFile<LocalWeatherData>("201503hourly.txt", true, ',', (o, c) =>
13 // c[1] data is in "20150301" format
14 o.Date = new DateTime(fastCSV.ToInt(c[1], 0, 4),
15 fastCSV.ToInt(c[1], 4, 2),
16 fastCSV.ToInt(c[1], 6, 2));
17 o.SkyCondition = c[4];
18 //if (o.Date.Day % 2 == 0)
Performance v1
Loading the https://www.ncdc.noaa.gov/orders/qclcd/QCLCD201503.zip file which has 4,496,263 rows on my machine as a relative comparison to other libraries:
- fastcsv : 11.20s 639Mb used
- nreco.csv : 6.62s 800Mb used
- **.net string.Split()** : 11.50s 638Mb used
- tinycsvparser : 34s 992Mb used
v2
Rewritten the internals:
- using a char buffer to read from the file instead of
File.ReadLines()
- using
Span
like MGSpan
data structure for .net4
- columns are only converted to string when used in the delegate
- fast create object IL instead of
new T()
Performance
- fastcsv net4: 6.27s 753Mb used
- fastcsv core : 6.51s 669Mb used