Search code examples
c#datatablecsvhelper

C# Pandas read_csv Equivalent


I am fairly familiar with pandas dataframes, but I am currently working with C# DataTables. I am trying to read a csv with a specified delimiter into a DataTable. In python this would be as simple as

import pandas 

df = pandas.read_csv(csvPath, delimiter = "|")

Is there an equivalent in C# something like

string csvPath = "myPath.csv";

DataTable dt = new DataTable();

dt.CsvHelper.CsvReader(csvPath, delimiter = "|");

I have looked at the CsvHelper Documentation, but the examples assume you have created an object that mirrors the csv. I my case I don't know what the CSV will look like and therefore will not be able to create a mapping class.

I am open to using something other than CsvHelper, I just want to make sure it is robust when dealing with delimiters other than ",".


Solution

  • Cinchoo ETL - an open source library available to do the conversion of CSV to DataTable easily with few lines of code

    using (var p = new ChoCSVReader("sample.csv").WithFirstLineHeader())
    {
        var dt = p.AsDataTable();
    }
    

    Checkout CodeProject article for some additional help.

    Disclaimer: I'm the author of this library.