Simple.CsvReader 8.0.0

dotnet add package Simple.CsvReader --version 8.0.0                
NuGet\Install-Package Simple.CsvReader -Version 8.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Simple.CsvReader" Version="8.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Simple.CsvReader --version 8.0.0                
#r "nuget: Simple.CsvReader, 8.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Simple.CsvReader as a Cake Addin
#addin nuget:?package=Simple.CsvReader&version=8.0.0

// Install Simple.CsvReader as a Cake Tool
#tool nuget:?package=Simple.CsvReader&version=8.0.0                

Simple.CsvReader

Simple.CsvReader is a lightweight and easy-to-use C# library for reading and parsing CSV (Comma-Separated Values) files. This library is designed to provide a straightforward way to handle CSV data in your C# projects, offering flexibility and simplicity.

Features

  • Easy Integration: Because the intended use is for 1-off assignments, all you really need to do is specify the fields to retrieve from a file and the types, create a CsvReader object, and read data.

  • Configurable: Customize the CSV parsing behavior with various options, Almost exclusively specifying delimiters.

  • Error Handling: None included!

Getting Started

Installation

You can install Simple.CsvReader via NuGet Package Manager Console:

Install-Package Simple.CsvReader

Or use the .NET CLI:

dotnet add package Simple.CsvReader

Usage

To use the Simple.CsvReader, we'll start with the assumption that your have a csv file where the first line contains column names, and the proceeding lines are rows with values. As an example:

FirstName, MiddleName, LastName
"John","Michael","Smith"
"Emma","Grace","Johnson"
"Alexander","James","Davis"

To use Simple.CsvReader, you'll first need to implement the RowMapper class and override MapRow. Be sure to use the "GetField" method, and pass in the value of the column heading that you want to parse:

public record Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
}

public class PersonMapper : RowMapper<Person>
{
    public override Person? MapRow(string[] fields)
    {
        return new Person()
        {
            FirstName = GetField(fields, "FirstName"),
            LastName = GetField(fields, "LastName"),
        };
    }
}

Create your CsvReader, passing in the full path of the file you want to read, the column header values, and the mapper you want to use, then call ReadAll to get your data!

CsvReader<Person> reader = new (
    fullPath: "A:\\Example\\filename.csv", 
    columnsToRead: new string[] { "FirstName", "LastName" }, 
    mapper: new PersonMapper()
);

IEnumerable<Person> fileData = reader.ReadAll();

foreach (var person in fileData) {
    Console.WriteLine($"{person.FirstName} {person.LastName}");
}

// Outputs:
// "John Smith"
// "Emma Johnson"
// "Alexander Davis"

For more detailed usage and configuration options, check out the source code. There's not much 😃

Contributions

Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to create an issue or submit a pull request.

License

This project is licensed under the GNU General Public License - see the (LICENSE)LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 143 5/24/2024
6.0.1 106 5/24/2024
6.0.0 126 1/26/2024