Mwspencer
Mwspencer

Reputation: 1183

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 ",".

Upvotes: 1

Views: 4331

Answers (2)

Cinchoo
Cinchoo

Reputation: 6322

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.

Upvotes: 2

user3188639
user3188639

Reputation:

You can use the CsvDataReader from this repository https://github.com/ttustonic/LightGBMSharp

There is a CsvDataReader in the Example directory, which is a standalone file, so you don't need the rest.

It implements a IDataReader interface and can be used to load DataTable.

Here's an example. Let's say that your csv file looks like this:

Id  Name    Age
1   Mark    100
2   Joe 32
3   Betty   55

THis code:

var dt = new DataTable();
using (var rdr = new CsvDataReader(file, true)) // set true if the csv has header line, false otherwise
{
    //rdr.ColumnTypes = new Type[] { typeof(int), typeof(string), typeof(int) }; Uncomment this if you know the structure of the csv
    dt.Load(rdr);
}
foreach (DataRow r in dt.Rows)
{
    for (int i = 0; i < dt.Columns.Count; i++)
        Console.Write($"{dt.Columns[i]}:{r[i]}  ");
    Console.WriteLine("");
}

will give the following output:

Id:1  Name:Mark  Age:100
Id:2  Name:Joe  Age:32
Id:3  Name:Betty  Age:55

Default delimiter is a TAB, which can be changed in the constructor.

Upvotes: 2

Related Questions