4

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?

6
  • 1
    Check out this previously answered thread. stackoverflow.com/questions/1050112/… Commented Aug 2, 2011 at 16:56
  • 1
    seems to be an exact duplicate of stackoverflow.com/questions/1050112/… Commented Aug 2, 2011 at 16:56
  • OOps: I see some one has already asked this question. What is suprising however, is that there does not seem to be an "out of the box" solution - this is quite strange to me, given the sheer number of classes in the .Net library - I would have thought CSV importing would come as "standard". Anyway, thanks for pointing me in the right direction. This question can be closed as superfulous (I don't have enough points to close it my self) Commented Aug 2, 2011 at 16:59
  • 1
    @oompahloompah: There's the built-in Microsoft.VisualBasic.FileIO.TextFieldParser class, but I prefer Sébastien Lorion's Fast CSV Reader regardless. Commented Aug 2, 2011 at 17:07
  • CSV importing does come standard using OdbcConnection,OdbcCommand,OdbcDataAdapter. I find well worthwhile to write a schema.ini before trying to read as otherwise Odbc has to guess at the type of each column. The schema also helps with getting the column names the way you want them. Commented Aug 2, 2011 at 20:22

2 Answers 2

9

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I was trying to avoid rolling my own :). CSV parsing is a real pain to get right!. I like the lightweight reader your link points to - it does all I need it to do. Thanks!
I am using LumenWorks framework for CSV, and was able to retrieve the fields but not sure how to write to a SQL query: stackoverflow.com/questions/24098812/… (a lot of people were kind enough to provide their own but With the library I already have it reading correctly, just need to write to a sql database)
Does the code work for tsv file as well, can I configure the delimiter?
1

I didn't find any built-in .NET (this is when I coded my solution in .NET 2.0) features that satisfied my needs, so I used the open source link below. I process about 36000 files a month, it works well and I've yet to have an issue.

CsvReader

1 Comment

The issue I am having is writing to a SQL table :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.