Helpwiz.FastCsvReader 1.0.6

Install-Package Helpwiz.FastCsvReader -Version 1.0.6
dotnet add package Helpwiz.FastCsvReader --version 1.0.6
<PackageReference Include="Helpwiz.FastCsvReader" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Helpwiz.FastCsvReader --version 1.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Helpwiz.FastCsvReader, 1.0.6"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Helpwiz.FastCsvReader as a Cake Addin
#addin nuget:?package=Helpwiz.FastCsvReader&version=1.0.6

// Install Helpwiz.FastCsvReader as a Cake Tool
#tool nuget:?package=Helpwiz.FastCsvReader&version=1.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Helpwiz.FastCsvReader

A fast compiled expression based csv reader.

Capabilities

  • Allows custom delimiter characters.
  • Allows quoted values with commas inside e.g. "My value, my second value and my third value"
  • Allows double escaped quotes e.g. "And his heart sang, like a bird: ""I love you!"""
  • Allows backslash escaped quotes e.g. "And his heart sang, like a bird: \"I love you!\""

Limitations

  • Does not allow multi-line values
  • Requires a header row (but this can be read from a separate file or synthesised if required).

Performance

Because the property access expressions are compiled, the library will load csv files at the maximum speed possible.

Getting started

Basic Usage

If you want the csv data to populate a class, make settable properties of all the header fields in the class, and the reader will read into that class

private string[] testData = new []
{ 
  "A, B, C, D", 
  "1,Two,3.0,"
};

private class TargetType 
{
  public int A { get; set; }
  
  public string B { get; set; }
  
  public double C { get; set; }
  
  public int? D { get; set; }
}

//...

public IEnumerable<TargetType> ReadElements() => FastCsvReader.ReadAs<TargetType>(testData);

Named properties

If you want the properties of the target type to differ in name from the header fields, you can use a [CsvProperty] attribute. Note header names are trimmed when matching to properties.

private string[] testData = new[]
{
  "A property, B property, C property, D property",
  "1,Two,3.0,"
};

private class TargetType 
{
  [CsvProperty("A property")]
  public int A { get; set; }
  
  [CsvProperty("B property")]
  public string B { get; set; }
  
  [CsvProperty("C property")]
  public double C { get; set; }
  
  [CsvProperty("D property")]
  public int? D { get; set; }
}

Data conversion and Custom Delimiters

Sensible defaults for string, int, double, DateTime, int?, double? and DateTime? are provided by default. You can override the default behaviour of this and extend to other datatypes as follows:

  var converter = new DefaultConverterSpec();
  converter.SetConverter<int?>(s =>
  {
    if (string.IsNullOrWhiteSpace(s)) return null;
    if (!int.TryParse(s, out int value)) return null;
    return value;
  });
  
  //Read the file with ¬ as the delimiter character and with custom handling for nullable integers.
  var result = FastCsvReader.ReadAs<DefaultData2>(testData3, '¬', converter).ToArray();

Additional Data

You can also load raw strings into your objects by implementing IAdditionalColumns on your object e.g.

private class TargetType : IAdditionalColumns
{
  //Data from "A" column loads here.
  public string A { get; set; }
  
  //Additional data loads here (all columns except A)
  public void WriteColumn(string header, string value)
  {
    //...
  }
}

Helpwiz

Helpwiz is an online marketplace for independent domestic cleaners. We allow clients to find a cleaner and book a clean in a simple, one-pass process. We have cleaners in many locations throughout the UK.

We provide this code for free, without any license contraints, but we do hope that, if you make use of the code, you will link back to us or otherwise give us a shout out.

  • .NETStandard 2.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
1.0.6 133 2/7/2021
1.0.5 221 11/26/2020
1.0.4 203 11/19/2020
1.0.3 213 11/18/2020
1.0.2 197 11/18/2020
1.0.1 159 11/17/2020
1.0.0 209 11/16/2020

Added additional debugging options for type converters.