FG.CsvParser 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package FG.CsvParser --version 1.0.2                
NuGet\Install-Package FG.CsvParser -Version 1.0.2                
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="FG.CsvParser" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FG.CsvParser --version 1.0.2                
#r "nuget: FG.CsvParser, 1.0.2"                
#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 FG.CsvParser as a Cake Addin
#addin nuget:?package=FG.CsvParser&version=1.0.2

// Install FG.CsvParser as a Cake Tool
#tool nuget:?package=FG.CsvParser&version=1.0.2                

FG.CsvParser

FG.CsvParser is a .NET Standard 2.1 library for parsing and writing CSV files. It provides a flexible and easy-to-use API for reading, writing, and querying CSV data.

Features

  • Read CSV files and convert them to JSON or a list of objects.
  • Write CSV content to files.
  • Configure CSV parsing options such as delimiter, row splitter, and encoding.
  • Query CSV files with custom filters.

Installation

To install FG.CsvParser, add the following package to your project:

Usage

Creating a CsvParser Instance

You can create a CsvParser instance using one of the static OpenFile methods:

  1. Open a CSV file with default settings:
var parser = CsvParser.OpenFile("path/to/your/file.csv");
  1. Open a CSV file and specify if it has a header row:
var parser = CsvParser.OpenFile("path/to/your/file.csv", hasHeader: true);
  1. Open a CSV file with a custom configuration:
var configuration = new CsvParserConfiguration
{
    HasHeader = true,
    Delimitter = ',',
    RowSplitter = "&#x0a;",
    Encoding = Encoding.UTF8
};
var parser = CsvParser.OpenFile("path/to/your/file.csv", configuration);

Reading CSV Content

You can read the CSV content and convert it to JSON or a list of objects:

  1. Reading CSV Content as JSON
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });

string? jsonContent = await parser.ReadAsJson();
Console.WriteLine(jsonContent);
  1. Reading CSV Content as a List of Objects
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });

List<MyDataClass> dataList = await parser.ReadAs<MyDataClass>();
foreach (var data in dataList)
{
    Console.WriteLine(data);
}

Writing CSV Content

You can write CSV content to a file:

  1. Writing CSV Content as a String
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });

string csvContent = "Column1,Column2\nValue1,Value2\nValue3,Value4";
await parser.WriteAsync(csvContent, append: false);

Console.WriteLine("CSV content written to file.");
  1. Writing a List of Objects to CSV
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });

var dataList = new List<MyDataClass>
{
    new MyDataClass { Column1 = "Value1", Column2 = 1 },
    new MyDataClass { Column1 = "Value2", Column2 = 2 }
};

await parser.WriteAsync(dataList, append: false);

Console.WriteLine("List of objects written to CSV file.");
  1. Appending CSV Content
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });

string csvContent = "Value5,Value6\nValue7,Value8";
await parser.WriteAsync(csvContent, append: true);

Console.WriteLine("CSV content appended to file.");

Querying CSV Content

You can query the CSV content with a custom filter:

var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });

await foreach (var item in parser.Query<MyDataClass>(data => data.Column2 > 100))
{
    Console.WriteLine(item);
}

Configuration

The CsvParserConfiguration class allows you to configure various options for the CSV parser:

var filePath = "path/to/your/csvfile.csv";

// Create a configuration for the CSV parser
var configuration = new CsvParserConfiguration
{
    HasHeader = true,
    Delimitter = ';',
    RowSplitter = "\n",
    Encoding = Encoding.UTF8
};

// Open the CSV file with the specified configuration
using var parser = CsvParser.OpenFile(filePath, configuration);

// Example: Reading CSV content as JSON
string? jsonContent = await parser.ReadAsJson();
Console.WriteLine("CSV content as JSON:");
Console.WriteLine(jsonContent);

// Example: Writing CSV content
var dataList = new List<MyDataClass>
{
    new MyDataClass { Column1 = "Value1", Column2 = 1 },
    new MyDataClass { Column1 = "Value2", Column2 = 2 }
};
await parser.WriteAsync(dataList, append: false);
Console.WriteLine("List of objects written to CSV file.");

License

This project is licensed under the MIT License. See the https://opensource.org/license/mit file for details.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.

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.18 71 1/20/2025
1.0.17 63 1/20/2025
1.0.16 64 1/20/2025
1.0.14 64 1/20/2025
1.0.6 57 1/18/2025
1.0.5 67 1/14/2025
1.0.4 67 1/13/2025
1.0.3 83 1/6/2025
1.0.2 76 1/6/2025
1.0.1 78 1/6/2025
1.0.0 79 1/6/2025