Csv 2.0.205
csv
Really simple csv library
This library targets .NET Standard 2.0, .NET 8.0, and .NET 9.0, with enhanced Span/Memory APIs available for .NET 8.0+.
Install
To install csv, use the following command in the Package Manager Console
PM> Install-Package Csv
Basic Usage
More examples can be found in the tests.
Reading a CSV file
// NOTE: Library assumes that the csv data will have a header row by default, see CsvOptions.HeaderMode
/*
# comments are ignored
Column name,Second column,Third column
First cell,second cell,
Second row,second cell,third cell
*/
var csv = File.ReadAllText("sample.csv");
foreach (var line in CsvReader.ReadFromText(csv))
{
// Header is handled, each line will contain the actual row data
var firstCell = line[0];
var byName = line["Column name"];
}
CsvReader also supports reading from a TextReader (CsvReader.Read(TextReader, CsvOptions)) or a Stream (CsvReader.ReadFromStream(Stream, CsvOptions))
For .NET 8.0+ the library exposes:
CsvReader.ReadAsyncandCsvReader.ReadFromStreamAsyncwhich returnIAsyncEnumerable<ICsvLine>CsvReader.ReadFromMemoryto read from aReadOnlyMemory<char>without allocating intermediate stringsCsvReader.ReadAsSpan,CsvReader.ReadFromStreamAsSpan, andCsvReader.ReadFromTextAsSpanwhich returnIEnumerable<ICsvLineSpan>for zero-allocation Span/Memory access to CSV dataCsvReader.ReadFromMemoryOptimizedwithCsvMemoryOptionsfor high-performance scenarios using ArrayPool-based buffering
High-performance reading with Span/Memory (.NET 8.0+)
var csv = File.ReadAllText("sample.csv");
foreach (var line in CsvReader.ReadFromTextAsSpan(csv))
{
// Access data as ReadOnlySpan<char> for zero allocations
ReadOnlySpan<char> firstCell = line.GetSpan(0);
ReadOnlySpan<char> byName = line.GetSpan("Column name");
// Or as ReadOnlyMemory<char> if you need to store references
ReadOnlyMemory<char> cellMemory = line.GetMemory(0);
}
CsvOptions can be used to configure the csv parsing:
var options = new CsvOptions // Defaults
{
RowsToSkip = 0, // Allows skipping of initial rows without csv data
SkipRow = (row, idx) => string.IsNullOrEmpty(row) || row[0] == '#',
Separator = '\0', // Autodetects based on first row
TrimData = false, // Can be used to trim each cell
Comparer = null, // Can be used for case-insensitive comparison for names
HeaderMode = HeaderMode.HeaderPresent, // Assumes first row is a header row
ValidateColumnCount = false, // Checks each row immediately for column count
ReturnEmptyForMissingColumn = false, // Allows for accessing invalid column names
Aliases = null, // A collection of alternative column names
AllowNewLineInEnclosedFieldValues = false, // Respects new line (either \r\n or \n) characters inside field values enclosed in double quotes.
AllowBackSlashToEscapeQuote = false, // Allows the sequence "\"" to be a valid quoted value (in addition to the standard """")
AllowSingleQuoteToEncloseFieldValues = false, // Allows the single-quote character to be used to enclose field values
NewLine = Environment.NewLine // The new line string to use when multiline field values are read (Requires "AllowNewLineInEnclosedFieldValues" to be set to "true" for this to have any effect.)
};
Writing a CSV file
With headers
var columnNames = new [] { "Id", "Name" };
var rows = new []
{
new [] { "0", "John Doe" },
new [] { "1", "Jane Doe" }
};
var csv = CsvWriter.WriteToText(columnNames, rows, ',');
File.WriteAllText("people.csv", csv);
/*
Writes the following to the file:
Id,Name
0,John Doe
1,Jane Doe
*/
Without headers
var rows = new []
{
new [] { "0", "John Doe" },
new [] { "1", "Jane Doe" }
};
// Convenience overload - no need to pass headers or skipHeaderRow
var csv = CsvWriter.WriteToText(rows);
File.WriteAllText("people.csv", csv);
/*
Writes the following to the file (no header row):
0,John Doe
1,Jane Doe
*/
Skipping headers
var columnNames = new [] { "Id", "Name" };
var rows = new []
{
new [] { "0", "John Doe" },
new [] { "1", "Jane Doe" }
};
// Pass null for headers and skipHeaderRow: true
// Column count is determined from the first data row
var csv = CsvWriter.WriteToText(null, rows, ',', skipHeaderRow: true);
Custom separator
var rows = new [] { new [] { "A", "B" }, new [] { "C", "D" } };
var csv = CsvWriter.WriteToText(rows, ';'); // semicolon separator
// Output: A;B
// C;D
CsvWriter also includes asynchronous overloads (WriteAsync and
WriteToTextAsync) which operate on IAsyncEnumerable<string[]> and support
passing a CancellationToken. For .NET 8.0+, memory-efficient overloads using
ReadOnlyMemory<char> are available.
Helper extensions
CsvReader provides extension methods to work with the returned
IEnumerable<ICsvLine>:
GetColumn(int columnNo)/GetColumn<T>(int columnNo, Func<string, T>)– extract a single column from all rows.GetBlock(int row_start = 0, int row_length = -1, int col_start = 0, int col_length = -1)– get a rectangular subset of the data.
Status
License
Showing the top 20 packages that depend on Csv.
| Packages | Downloads |
|---|---|
|
Zebra.Printer.SDK
The Zebra Link-OS SDK provides a powerful set of APIs enabling creation of apps that take full advantage of the printer's operating system features including, connectivity, printing and management tasks.
|
7 |
|
Zebra.Printer.SDK
The Zebra Link-OS SDK provides a powerful set of APIs enabling creation of apps that take full advantage of the printer's operating system features including, connectivity, printing and management tasks.
|
10 |
.NET 8.0
- No dependencies.
.NET 9.0
- No dependencies.
.NET Standard 2.0
- No dependencies.
| Version | Downloads | Last updated |
|---|---|---|
| 2.0.205 | 2 | 10/23/2025 |
| 2.0.170 | 14 | 05/23/2025 |
| 2.0.128 | 12 | 03/14/2025 |
| 2.0.93 | 17 | 12/16/2024 |
| 2.0.87 | 22 | 01/08/2025 |
| 2.0.84 | 19 | 01/03/2025 |
| 2.0.80 | 21 | 01/03/2025 |
| 2.0.76 | 20 | 01/04/2025 |
| 2.0.67 | 21 | 01/08/2025 |
| 2.0.65 | 18 | 01/03/2025 |
| 2.0.64 | 17 | 01/03/2025 |
| 2.0.62 | 17 | 01/03/2025 |
| 2.0.61 | 19 | 01/02/2025 |
| 1.0.58 | 17 | 01/04/2025 |
| 1.0.57 | 16 | 01/02/2025 |
| 1.0.55 | 15 | 01/03/2025 |
| 1.0.53 | 18 | 01/03/2025 |
| 1.0.52 | 20 | 01/02/2025 |
| 1.0.51 | 21 | 01/04/2025 |
| 1.0.48 | 18 | 01/04/2025 |
| 1.0.40 | 21 | 01/04/2025 |
| 1.0.39 | 17 | 01/02/2025 |
| 1.0.38 | 17 | 01/03/2025 |
| 1.0.35 | 18 | 01/04/2025 |
| 1.0.34 | 19 | 01/02/2025 |
| 1.0.31 | 20 | 01/04/2025 |
| 1.0.29 | 17 | 01/03/2025 |
| 1.0.12 | 18 | 01/03/2025 |
| 1.0.11 | 19 | 01/03/2025 |
| 1.0.10 | 18 | 01/03/2025 |
| 1.0.9 | 18 | 01/03/2025 |
| 1.0.7 | 17 | 01/04/2025 |
| 1.0.6 | 18 | 01/03/2025 |
| 1.0.4 | 17 | 01/03/2025 |
| 1.0.0 | 19 | 01/03/2025 |