Reading csv file into a DataTable using C#?

oompahloompah picture oompahloompah · Aug 2, 2011 · Viewed 8.9k times · Source

I have a number of Python scripts I wrote a while back, to do some data munging. I need to 'port' some of those scripts to C#.

Python provides a CSV module which facilitates importing CSV data from file into a dictionary. I want to have the same functionality in my library, but since I am new to C#, decided to come in here to ask for the best practices way to import CSV data into a DataTable.

Do I roll my own, or is there a 'CSV module' ala Python?

Answer

LukeH picture LukeH · Aug 2, 2011

I wouldn't try to roll your own. You'll have your work cut out trying to cope with all the weird corner-cases that CSV files can throw at you.

I would recommend Sébastien Lorion's Fast CSV Reader instead:

using (var csv = new CachedCsvReader(new StreamReader(filePath), true))
{
    DataTable Table = new DataTable();
    Table.Load(csv);
}