CSV support in .NET Core #37711
-
I apologize if this is not the correct place to make such a request, but I believe it is really past time for .NET to provide CSV layout functionality natively. Developers are currently at the mercy of doing it manually or using CsvHelper, Lumenworks or a plethora of different packages, each with their own set of peculiarities. Admittedly 'standard' should be a loose term when referring to CSV, but MS could do a lot with their own implementation to cut down the confusion. I believe there was some support for it in VB, and some messy ways of referencing it in C#, but first class support for it would be spectacular. |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments 53 replies
-
@sparticus1701 have you seen this https://devblogs.microsoft.com/dotnet/an-introduction-to-dataframe/ ? |
Beta Was this translation helpful? Give feedback.
-
Does DataFrame help @sparticus1701 ? |
Beta Was this translation helpful? Give feedback.
-
No, it doesn't. |
Beta Was this translation helpful? Give feedback.
-
Generally we want to support community libraries. Could you share more about the issues you have with them? |
Beta Was this translation helpful? Give feedback.
-
Have you looked at the |
Beta Was this translation helpful? Give feedback.
-
It's simply split with |
Beta Was this translation helpful? Give feedback.
-
It's far past time for .NET to have a proper CSV parsing library.
The BCL should fix this shortcoming and make a standard CSV parser implementation everyone can use. |
Beta Was this translation helpful? Give feedback.
-
.... this depends completely on audience. In a web api, current trend is to JSON (hence CSV is commonly used with relational databases (and hence Excel) - but to represent one result set at a time (where the format will be constant). Note that pretty much all the major ones have built-in export/import tools to handle this, which usually means you let the database do that for you. |
Beta Was this translation helpful? Give feedback.
-
Somewhat related: https://github.com/kevin-montrose/Cesil |
Beta Was this translation helpful? Give feedback.
-
Also related: |
Beta Was this translation helpful? Give feedback.
-
@danmoseley If a more concrete proposal is made for this will the BCL team triage it and vote up or down whether to undertake this? |
Beta Was this translation helpful? Give feedback.
-
I have many times wanted to see CSV data be more of a first-party supported datastructure, but the need is usually handled by very few lines of code, but I have come across issues when I am presenting that on an API, and even CsvHelper, (which is my opinion is currently the best library for CSV), has some frustrations. Mostly what I want is to get the CSV-data into a DataTable-like structure that is flexible and typed, (but only on actual read), and it has a coordinate system so errors can be expressed as "Row 5, Cell 15". Problem is that the enormous amouts of Cultures What the user experience might be: var document = new CsvDocument();
// Populate document
document.GetValue<decimal>(12, 15); Incomplete example structure: public class CsvCell
{
public string? Value { get; set; }
public TypeCode Type { get; set; }
}
public class CsvCell<T> : CsvCell
{
public T GetValue()
{
return default;
}
}
public class CsvRow : List<CsvCell>
{
}
public class CsvDocument : List<CsvRow>
{
private readonly CsvOptions _options;
public CsvDocument(CsvOptions options)
{
_options = options;
}
}
public class CsvOptions
{
public readonly static CsvOptions Default = new();
public string Delimiter { get; set; } = ";";
public string QuoteStart { get; set; } = "\"";
public string QuoteEnd { get; set; } = "\"";
public string NewLine { get; set; } = Environment.NewLine;
public bool HasHeaderRow { get; set; } = true;
public bool IgnoreEmptyLines { get; set; } = true;
}
public static class CsvParser
{
public static CsvRow ParseRow(string source, CsvOptions options = null)
{
options ??= CsvOptions.Default;
var row = new CsvRow();
// QuoteStart and QuoteEnd is not respected so a delimiter or line-break in a quote will mess things up
var cellValues = source.Split(options.Delimiter);
foreach (var cellValue in cellValues)
{
var cell = new CsvCell();
cell.Value = cellValue;
row.Add(cell);
}
return row;
}
public static CsvDocument ParseDocument(string source, CsvOptions options = null)
{
options ??= CsvOptions.Default;
var document = new CsvDocument(options);
// QuoteStart and QuoteEnd is not respected so a delimiter or line-break in a quote will mess things up
var rowValues = source.Split(options.NewLine);
// Header is not handled
foreach (var rowValue in rowValues)
{
if (options.IgnoreEmptyLines && string.IsNullOrWhiteSpace(rowValue))
continue;
var row = CsvParser.ParseRow(rowValue, options);
document.Add(row);
}
return document;
}
} |
Beta Was this translation helpful? Give feedback.
-
While you guys start planning .NET 10... it would be a great time to reconsider adding CSV parsing into .NET proper. This is still, by far, the biggest gap in the .NET BCL, in my opinion. If I had a dollar for the number of times I've seen devs (incorrectly) implement CSV parsing with a simple String.Split() I'd be retired by now. |
Beta Was this translation helpful? Give feedback.
@scottdorman
The key part is that the platform itself took a dependency on JSON, which isn't the case for CSV.
JSON isn't better because we added it to the platform; the primary benefit of doing JSON in the platform was avoiding conflicts in user graphs involving JSON.NET as well as improving the performance. We discussed doing the latter in JSON.NET directly bu…