开发者

Reading csv file into a DataTable using C#?

开发者 https://www.devze.com 2023-03-25 06:32 出处:网络
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#.

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 rol开发者_JS百科l my own, or is there a 'CSV module' ala Python?


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);
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号