Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/j0nimost/kafa
A fast easy to use csv,tsv file parser
https://github.com/j0nimost/kafa
csv dotnet rfc-4180 tsv
Last synced: 5 days ago
JSON representation
A fast easy to use csv,tsv file parser
- Host: GitHub
- URL: https://github.com/j0nimost/kafa
- Owner: j0nimost
- License: mit
- Created: 2023-10-03T05:11:10.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-21T04:45:36.000Z (about 1 year ago)
- Last Synced: 2025-01-20T23:56:58.366Z (7 days ago)
- Topics: csv, dotnet, rfc-4180, tsv
- Language: C#
- Homepage:
- Size: 167 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Kafa
A fast easy to use csv,tsv file parser. It has a low memory footprint with alot of optimizations to be done.Kafa is also RFC-4180 Compliant [docs](https://www.rfc-editor.org/rfc/rfc4180)
![Build Status](https://github.com/j0nimost/Kafa/actions/workflows/dotnet.yml/badge.svg)
🚧 UNDER ACTIVE DEVELOPMENT 🚧
### How To Read
There are two options;
- `RowEnumerable`
- `IEnumerable`#### `RowEnumerable`
For fast performance with minimal over head the RowEnumerable allows you to iterate over the CSV rows yourself.
Example:
```c#
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));using var rows = Kafa.Read(stream);
int rowCount =0, colCount = 0;
foreach (var row in rows)
{
foreach (var col in row.Cols)
{
Debug.WriteLine(col.ToString());
colCount++;
}
rowCount++;
}
```This offers you the granular control over each element. The API offers a list of extension Methods for the `Col` struct which allows
you to Parse to any given type; `ParseInt()`, `ParseLong()`, `ParseFloat()`, `ParseDateTime()` etc.Example:
```c#
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));using var rows = Kafa.Read(stream);
int sum =0;
foreach (var row in rows)
{
foreach (var col in row.Cols)
{
sum += col.ParseInt();
}
}
```To also make it easier you can jump to a specific Row and Col
Example:
```c#
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));using var rows = Kafa.Read(stream);
var result = rows[0].Cols[0].ToString();
// or
var result2 = rows[new Index(0)].Cols[new Index(0)].ToString();
// or
var result3 = row[0].Cols["date"].ToString(); // read Column by Name
```
#### `IEnumerable`
Understanding that many users want a simple easy to use experience the library allows
you to parse directly into a type.Example:
```c#
var stream = new MemoryStream(Encoding.UTF8.GetBytes(data));
var result = await Kafa.ReadAsync(stream);
```
To make it easier to parse a file Kafa offers an Attribute Type that can match a column name or the column index.Example:
```c#
class CSVDataWithAttributes
{
[KafaColumn("date")]
public DateTime Date { get; set; }
[KafaColumn(1)]
public double Open { get; set; }
[KafaColumn(2)]
public double High { get; set; }
[KafaColumn(3)]
public double Low { get; set; }
[KafaColumn(4)]
public double Close { get; set; }
[KafaColumn(5)]
public int Volume { get; set; }
[KafaColumn("name")]
public string Name { get; set; }
}
```
### How To Write
You can Write to;
- `IBufferWriter`
- `ReadOnlySpan`
- `Stream`
- `File`Behind the curtains Kafa is using `KafaPooledWriter` a pooled IBufferWriter to write to, unless explicitly provided a custom one using
`Write(IBufferWriter bufferWriter, List entities, KafaOptions options = null)` Method.```c#
var csvs = new List()
{
new CsvData{ Date = DateTime.Parse("10/10/2023 4:08:38 PM"), Open=12.45, Close=12.99, High=13.00, Low=12.1, Name="AMZN", Volume=1233435512},
new CsvData{ Date = DateTime.Parse("10/10/2023 4:08:38 PM"), Open=12.45, Close=12.99, High=13.00, Low=12.1, Name="AMZN", Volume=1233435512}
};
// get a ReadOnlySpan
var spanOfbytes = await Kafa.Write(csvs);
string result = Encoding.UTF8.GetString(spanOfbytes);// or
// write to an Stream
using var stream = await Kafa.WriteToStreamAsync(csvs);// or
// Write to a IBufferWriter for zero allocation writes
var arrayWriter = new ArrayBufferWriter();
Kafa.Write(arrayWriter, csvs);
var str = Encoding.UTF8.GetString(arrayWriter.WrittenSpan);
// or
// write directly to a file
await Kafa.WriteToFileAsync(csvs,@"C:\Users\ADMIN\Documents");```
Both writing and reading support Attributes
### Support
Only .Net Core 7 and >
### Author
John Nyingi