CSV Parsing Options with .Net

CSV Parsing Options with .NET

Did you try searching for an already-existing .NET CSV parser? This one claims to handle multi-line records significantly faster than OLEDB.

Parsing CSV files in C#, with header

Let a library handle all the nitty-gritty details for you! :-)

Check out FileHelpers and stay DRY - Don't Repeat Yourself - no need to re-invent the wheel a gazillionth time....

You basically just need to define that shape of your data - the fields in your individual line in the CSV - by means of a public class (and so well-thought out attributes like default values, replacements for NULL values and so forth), point the FileHelpers engine at a file, and bingo - you get back all the entries from that file. One simple operation - great performance!

Parse Delimited CSV in .NET

From here:

Encoding fileEncoding = GetFileEncoding(csvFile);
// get rid of all doublequotes except those used as field delimiters
string fileContents = File.ReadAllText(csvFile, fileEncoding);
string fixedContents = Regex.Replace(fileContents, @"([^\^,\r\n])""([^$,\r\n])", @"$1$2");
using (CsvReader csv =
new CsvReader(new StringReader(fixedContents), true))
{
// ... parse the CSV

.NET Csv Parser

You shouldn't reinvent the wheel.
You can try the CSVHelper library https://joshclose.github.io/CsvHelper/

One line of code to achieve what you are trying in many lines:

var records = csv.GetRecords<dynamic>();

You can find documentation here.

Parsing csv files with specific column delimiter & string enclosure using the CsvHelper library

This seems to work, using CsvHelper:

var textToParse = @"SupplierSku,CatIds,StockStatus,Active
%ADA-BB-124%|4,5,1|%AV%|1
%XAS-E4-S11%|97,41,65|%OS%|0";

string supplierSku;
string stockStatus;

using (var stringReader = new StringReader(textToParse))
{
using (var reader = new CsvReader(stringReader))
{
reader.Configuration.Delimiter = ",";
reader.Configuration.HasHeaderRecord = true; // If there is no header, set to false.

while (reader.Read())
{
supplierSku = reader.GetField("SupplierSku"); // Or reader.GetField(0)
stockStatus = reader.GetField("StockStatus"); // Or reader.GetField(2)

Console.WriteLine($"SKU: {supplierSku}; Status: {stockStatus}");
}
}
}

However, it doesn't automatically trim/remove the quote characters - you can easily do that yourself using Trim() or Substring(). There is a little more manual effort involved, but it is still easier than doing it manually.

Commercial .NET CSV Parser/Library

CSVReader did the trick for us.

Uploading and parsing a csv file in C#/Core MVC

Your first file stream is still open in your first using and you try to read it again with TextFieldParser

    private Dictionary<string, string[]> LoadData(IFormFile file)
{
// Verify that the user selected a file
if (file != null && file.Length > 0)
{
string wwwPath = this.environment.WebRootPath;
// string contentPath = this.environment.ContentRootPath;

string path = Path.Combine(wwwPath, "WeeklySchedules");

if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

string fileName = Path.GetFileName(file.FileName);

using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
file.CopyTo(stream);
}

// System.Threading.Thread.Sleep(1000);
using (TextFieldParser parser = new TextFieldParser(Path.Combine(path, fileName)))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");

Dictionary<string, string[]> parsedData = new Dictionary<string, string[]>();

while (!parser.EndOfData)
{
// Process row
string[] fields = parser.ReadFields();
int count = 0;

if (count++ == 0)
{
continue;
}

var pickup = fields[0];
var pickupDate = fields[1];
var dropoff = fields[2];
var dropoffDate = fields[3];
var driver = fields[7];

var pickupTime = DateTime.Parse(pickupDate).ToLongTimeString();
// string[] data =
}
}
}

return null;
}



Related Topics



Leave a reply



Submit