Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stevehansen/csv
Really simple csv library
https://github.com/stevehansen/csv
csharp csv netstandard
Last synced: about 12 hours ago
JSON representation
Really simple csv library
- Host: GitHub
- URL: https://github.com/stevehansen/csv
- Owner: stevehansen
- License: mit
- Created: 2015-09-19T20:47:29.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-05T09:41:57.000Z (2 months ago)
- Last Synced: 2024-11-03T02:37:02.227Z (10 days ago)
- Topics: csharp, csv, netstandard
- Language: C#
- Homepage:
- Size: 96.7 KB
- Stars: 205
- Watchers: 8
- Forks: 43
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# csv
Really simple csv library## 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
```csharp
// 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)`)
`CsvOptions` can be used to configure the csv parsing:
```csharp
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
```csharp
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
*/
```## Build status
[![Build status](https://ci.appveyor.com/api/projects/status/d1m0vu1n7idsk7uu?svg=true)](https://ci.appveyor.com/project/SteveHansen/csv)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fstevehansen%2Fcsv.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fstevehansen%2Fcsv?ref=badge_shield)
[![codecov](https://codecov.io/gh/stevehansen/csv/branch/master/graph/badge.svg)](https://codecov.io/gh/stevehansen/csv)## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fstevehansen%2Fcsv.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fstevehansen%2Fcsv?ref=badge_large)