KCSV 1.1.0

dotnet add package KCSV --version 1.1.0
NuGet\Install-Package KCSV -Version 1.1.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="KCSV" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KCSV --version 1.1.0
#r "nuget: KCSV, 1.1.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 KCSV as a Cake Addin
#addin nuget:?package=KCSV&version=1.1.0

// Install KCSV as a Cake Tool
#tool nuget:?package=KCSV&version=1.1.0

KCSV

A simple-to-use reliable CSV-parsing library.

Available on Nuget

Contents

Features

  • Uses NetStandard 2.0 for high compatibility
  • Loads or streams content from a file
    • Both CSV and Tabs supported
    • Works with both Linux/Mac/Unix and Windows line endings
  • Accepts content as an array of rows as strings
    • Both CSV and Tabs supported
  • Splits into a collection of rows with:
    • Original source row number
    • A collection of cells with each having:
      • A flag for if there were double-quotes in the input
      • The content, raw and pre-formatted with any quotes
  • Checks for jaggedness in the parsed table
    • Defined as rows with less cells than the widest row
    • Optionally removes jaggedness by padding with empty cells
  • Optional pre-formatted row output
    • Both CSV and Tabs supported
    • Ready for writing back out, complete with quoting
  • Flexible and accurate
    • Understands double-quoted and plain cells
    • Both CSV and Tabs supported
    • Allows embedded commas where quoted
    • Accepts unquoted full text (stops at a comma/tab)
    • Includes comprehensive tests

Usage

Remember that despite the name Tab-delimited is also supported. Specifying the delimiter is optional; Comma is the default.

Loading CSV/Tab content from a file

This loads all the rows in one hit.

var table = Parser.LoadTable("file.csv", Delimiters.Comma);
var table = Parser.LoadTable("file.tab", Delimiters.Tab);

Streaming CSV/Tab content from a file

This uses a forward-only stream of rows fetched one at a time from a file on demand. Lighter on resources.

using (var stream = Parser.StreamTable("file.csv", Delimiters.Comma))
{
  var rowNumber = 0;
  while(stream.EndOfStream == false)
  {
    var row = stream.NextRow();
    Console.WriteLine($"Row {++rowNumber} has {row.CellCount} cell(s)")
  }
}

Passing in CSV/Tab content from strings

var csv = new string[]
{
  "ID,Name",
  "1, \"Davey Jones\"",
  "2, \"Don Atello\"",
};
var table = Parser.FromString(csv, Delimiters.Comma);

The Table model, methods, and hierarchy

  • Use LoadTable(...), StreamTable, or FromString(...)
    • See the examples above for details
  • All data whether loaded or taken from strings ends in a Table
  • The Table has row stats and a collection of Rows
  • A Row has cell stats and contents
Table
  Rows          - The individual rows of the CSV
  RowCount      - The number of rows found in the CSV
  MinCellCount  - The cell count for the narrowest row
  MaxCellCount  - The cell count for the widest row
  IsJagged      - Do any rows have less cells than others?

  SquareOff()   - Ensures all rows have the same amount of cells by adding extra (empty) ones where necessary

Rows is a readonly collection of type Row.

Row
  RowNumber   - The original CSV row number
  Cells       - The parsed cells from the row
  CellCount   - The number of cells in the row

  AsCSV()     - Returns the row as a CSV string, with cells quoted as per the original file
  AsTabbed()  - Returns the row as a Tab-delimited string, with cells quoted as per the original file

Cells is a readonly collection of type Cell.

Cell
  IsQuoted   - Were there double-quotes in the input CSV?
  Text       - The text, with any quoting from the original CSV content removed
  Formatted  - The text, wrapped in quotes if the originalCSV content included them

For developers working on KCSV itself

If you only intend making use of KCSV in your own projects read no further.

Running the tests

This library is NetStandard 2 for compatibility. The tests however were created under DotNet 8 and no guarantees are offered for earlier versions.

  • Open a terminal/command prompt
  • Navigate into the solution folder
  • Run dotnet test
  • Run dotnet test -v n for more detail

Creating a new version for Nuget

The KCSV/KCSV.csproj file contains Nuget settings. Within that file, update the version number then create the Nuget package:

cd KCSV
dotnet build -c Release
dotnet pack -c Release

The pack command shouldn't be needed; the build should also create the package. The output from build and pack will advise where the .nupkg package file is. This package file is what gets uploaded in the Nuget UI.

Forcing another project to get the latest from Nuget

It sometimes takes a while for a new version to be available after pushing. You may be able to speed up the process:

cd <other-project>
dotnet restore --no-cache
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.1.0 83 4/15/2024
1.0.0 84 4/14/2024