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?

tmthydvnprt
  • 10,398
  • 8
  • 52
  • 72
oompahloompah
  • 9,087
  • 19
  • 62
  • 90
  • 1
    Check out this previously answered thread. http://stackoverflow.com/questions/1050112/how-to-read-a-csv-file-into-a-net-datatable – Sinsanator Aug 02 '11 at 16:56
  • 1
    seems to be an exact duplicate of http://stackoverflow.com/questions/1050112/how-to-read-a-csv-file-into-a-net-datatable – Matten Aug 02 '11 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) – oompahloompah Aug 02 '11 at 16:59
  • 1
    @oompahloompah: There's the built-in [`Microsoft.VisualBasic.FileIO.TextFieldParser`](http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx) class, but I prefer [Sébastien Lorion's Fast CSV Reader](http://www.codeproject.com/KB/database/CsvReader.aspx) regardless. – LukeH Aug 02 '11 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. – SeaDrive Aug 02 '11 at 20:22
  • Re my Aug 2 comment: not true for Windows 7. Microsoft dropped the ODBC text driver. – SeaDrive Aug 16 '11 at 19:33

2 Answers2

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);
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 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! – oompahloompah Aug 02 '11 at 17:07
  • I am using LumenWorks framework for CSV, and was able to retrieve the fields but not sure how to write to a SQL query: http://stackoverflow.com/questions/24098812/how-to-take-a-csv-field-and-write-to-columns-in-sql (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) – SearchForKnowledge Jun 09 '14 at 12:51
  • Does the code work for tsv file as well, can I configure the delimiter? – Shaohao Jul 05 '16 at 00:35
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

Eric H
  • 1,759
  • 1
  • 11
  • 14