World's Fastest .NET CSV Parser. Modern, minimal, fast, zero allocation, reading and writing of separated values (`csv`, `tsv` etc.). Cross-platform, trimmable
World's Fastest .NET CSV Parser. Modern, minimal, fast, zero allocation, reading and writing of separated values (`csv`, `tsv` etc.). Cross-platform, trimmable
Modern, minimal, fast, zero allocation, reading and writing of separated values
(csv, tsv etc.). Cross-platform, trimmable and AOT/NativeAOT compatible.
Featuring an opinionated API design and pragmatic implementation targeted at
machine learning use cases.
⭐ Please star this project if you like it. ⭐
** Modern** - utilizes features such as
Span<T>,
Generic Math
(ISpanParsable<T>/
ISpanFormattable),
ref struct,
ArrayPool<T>
and similar from .NET 7+ and C#
11+ for a modern
and highly efficient implementation.
** Minimal** - a succinct yet expressive API with few options and no hidden changes to input or output. What you read/write is what you get. E.g. by default there is no "automatic" escaping/unescaping of quotes or trimming of spaces. To enable this see SepReaderOptions and Unescaping and Trimming. See SepWriterOptions for Escaping.
** Fast** - blazing fast with both architecture specific and cross-platform SIMD vectorized parsing incl. 64/128/256/512-bit paths e.g. AVX2, AVX-512 (.NET 8.0+), NEON. Uses csFastFloat for fast parsing of floating points. See detailed benchmarks for cross-platform results.
️ Multi-threaded - unparalleled speed with highly efficient parallel CSV parsing that is up to 35x faster than CsvHelper, see ParallelEnumerate and benchmarks.
** Async support** - efficient ValueTask based async/await support.
Requires C# 13.0+ and for .NET 9.0+ includes SepReader implementing
IAsyncEnumerable<>. See Async Support for details.
️ Zero allocation - intelligent and efficient memory management allowing for zero allocations after warmup incl. supporting use cases of reading or writing arrays of values (e.g. features) easily without repeated allocations.
✅ Thorough tests - great code coverage and focus on edge case testing incl. randomized fuzz testing.
** Cross-platform** - works on any platform, any architecture supported by NET. 100% managed and written in beautiful modern C#.
✂️ Trimmable and AOT/NativeAOT compatible - no problematic reflection or dynamic code generation. Hence, fully trimmable and Ahead-of-Time compatible. With a simple console tester program executable possible in just a few MBs.
️ Opinionated and pragmatic - conforms to the essentials of RFC-4180, but takes an opinionated and pragmatic approach towards this especially with regards to quoting and line ends. See section RFC-4180.
Example | Naming and Terminology | API | Limitations and Constraints | Comparison Benchmarks | Example Catalogue | RFC-4180 | FAQ | Public API Reference
…For more examples, incl. how to write and read objects (e.g. records) with
escape/unescape support, see Example Catalogue.
Sep uses naming and terminology that is not based on RFC-4180, but
is more tailored to usage in machine learning or similar. Additionally, Sep
takes a pragmatic approach towards names by using short names and abbreviations
where it makes sense and there should be no ambiguity given the context. That
is, using Sep for Separator and Col for Column to keep code succinct.
| Term | Description |
|---|---|
Sep |
Short for separator, also called delimiter. E.g. comma (,) is the separator for the separated values in a csv-file. |
Header |
Optional first row defining names of columns. |
Row |
A row is a collection of col(umn)s, which may span multiple lines. Also called record. |
Col |
Short for column, also called field. |
Line |
Horizontal set of characters until a line ending; \r\n, \r, \n. |
Index |
0-based that is RowIndex will be 0 for first row (or the header if present). |
Number |
1-based that is LineNumber will be 1 for the first line (as in notepad). Given a row may span multiple lines a row can have a From line number and a ToExcl line number matching the C# range indexing syntax [LineNumberFrom..LineNumberToExcl]. |
Besides being the succinct name of the library, Sep is both the main entry
point to using the library and the container for a validated separator. That is,
Sep is basically defined as:
public readonly record struct Sep(char Separator);The separator char is validated upon construction and is guaranteed to be
within a limited range and not being a char like " (quote) or similar. This
can be seen in src/Sep/Sep.cs. The separator is constrained
also for internal optimizations, so you cannot use any char as a separator.
⚠ Note that all types are within the namespace nietras.SeparatedValues and not
Sep since it is problematic to have a type and a namespace with the same name.
To get started you can use Sep as the static entry point to building either a
reader or writer. That is, for SepReader:
using var reader = Sep.Reader().FromFile("titanic.csv");where .Reader() is a convenience method corresponding to:
using var reader = Sep.Auto.Reader().FromFile("titanic.csv");where Sep? Auto => null; is a static property that returns null for a
nullable Sep to signify that the separator should be inferred from the first
row, which might be a header. If the first row does not contain any of the by
default supported separators or there are no rows, the default separator will be
used.
⚠ Note Sep uses ; as the default separator, since this is what was used in an
internal proprietary library which Sep was built to replace. This is also to
avoid issues with comma , being used as a decimal separator in some locales.
Without having to resort to quoting.
If you want to specify the separator you can write:
using var reader = Sep.New(',').Reader().FromFile("titanic.csv");or
var sep = new Sep(',');
using var reader = sep.Reader().FromFile("titanic.csv");Similarly, for SepWriter:
using var writer = Sep.Writer().ToFile("titanic.csv");or
using var writer = Sep.New(',').Writer().ToFile("titanic.csv");where you have to specify a valid separator, since it cannot be inferred. To
facilitate easy flow of the separator and CultureInfo both SepReader and
SepWriter expose a Spec property of type SepSpec that simply defines those
two. This means you can write:
using var reader = Sep.Reader().FromFile("titanic.csv");
using var writer = reader.Spec.Writer().ToFile("titanic-survivors.csv");where the writer then will use the separator inferred by the reader, for
example.
In general, both reading and writing follow a similar pattern:
Sep/Spec => SepReaderOptions => SepReader => Row => Col(s) => Span/ToString/Parse
Sep/Spec => SepWriterOptions => SepWriter => Row => Col(s) => Set/Formatwhere each continuation flows fluently from the preceding type. For example,
Reader() is an extension method to Sep or SepSpec that returns a
SepReaderOptions. Similarly, Writer() is an extension method to Sep or
SepSpec that returns a SepWriterOptions.
SepReaderOptions and
SepWriterOptions are optionally configurable.
That and the APIs for reader and writer is covered in the following sections.
For a complete example, see the example above or the ReadMeTest.cs.
⚠ Note that it is important to understand that Sep Row/Col/Cols are ref structs
(please follow the ref struct link and understand how this limits the usage of
those). This is due to these types being simple facades or indirections to the
underlying reader or writer. That means you cannot use LINQ or create an array
of all rows like reader.ToArray(). While for .NET9+ the reader is now
IEnumerable<> since ref structs can now be used in interfaces that have
where T: allows ref struct
this still does not mean it is LINQ compatible. Hence, if you need store per row
state or similar you need to parse or copy to different types instead. The same
applies to Col/Cols which point to internal state that is also reused. This
is to avoid repeated allocations for each row and get the best possible
performance, while still defining a well structured and straightforward API that
guides users to relevant functionality. See Why SepReader Was Not IEnumerable
Until .NET 9 and Is Not LINQ
Compatible
for more.
⚠ For a full overview of public types and methods see Public API Reference.
SepReader API has the following structure (in pseudo-C# code):
using var reader = Sep.Reader(o => o).FromFile/FromText/From...;
var header = reader.Header;
var _ = header.IndexOf/IndicesOf/NamesStartingWith...;
foreach (var row in reader)
{
var _ = row[colName/colNames].Span/ToString/Parse<T>...;
var _ = row[colIndex/colIndices].Span/ToString/Parse<T>...;
}That is, to use SepReader follow the points below:
Sep or use default automatically inferred separator.SepReaderOptions. For
example, if a csv-file does not have a header this can be configured via:Sep.Reader(o => o with { HasHeader = false })string), TextWriter, etc. via From
extension methods.GT_ use:var colNames = header.NamesStartingWith("GT_");
var colIndices = header.IndicesOf(colNames);Sep internally handles pooled allocation and reuse of arrays for
multiple columns.Span to access the column directly as a ReadOnlySpan<char>. Or use
ToString to convert to a string. Or use Parse<T> where T : ISpanParsable<T> to parse the column chars to a specific type.The following options are available:
…While great care has been taken to ensure Sep unescaping of quotes is
No open issues yet, or sync has not completed.