高效地读取C#中的.csv文件? [英] Read a .csv file in c# efficiently?

查看:813
本文介绍了高效地读取C#中的.csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这种方式读取巨大的csv文件(每个文件约35万行):

I'm reading huge csv files (about 350K lines by file) using this way:

StreamReader readFile = new StreamReader(fi);
    string line;
    string[] row;
    readFile.ReadLine();
    while ((line = readFile.ReadLine()) != null)
    {
        row = line.Split(';');
        x=row[1];
        y=row[2];
        //More code and assignations here...
    }
    readFile.Close();
}

这里的要点是每月一个月的每一行逐行读取一个大文件可能会很慢,我认为这必须是另一种方法来更快地读取文件.

The point here is that reading line by line a huge file for every day of the month may be slow and I think that it must be another method to do it faster.

推荐答案

方法1

通过使用LINQ:

var Lines = File.ReadLines("FilePath").Select(a => a.Split(';'));
var CSV = from line in Lines 
          select (line.Split(',')).ToArray();

方法2

Jay Riggs 在此处所述

这是一个很棒的类,它将使用数据的结构将CSV数据复制到数据表中以创建DataTable:

Here's an excellent class that will copy CSV data into a datatable using the structure of the data to create the DataTable:

用于平面文件的可移植且高效的通用解析器

易于配置和使用.我敦促你看看.

It's easy to configure and easy to use. I urge you to take a look.

方法3

滚动您自己的CSV阅读器是浪费时间,除非您正在阅读的文件得到非常简单的保证.改用预先存在且经过测试的实现方式.

Rolling your own CSV reader is a waste of time unless the files that you're reading are guaranteed to be very simple. Use a pre-existing, tried-and-tested implementation instead.

这篇关于高效地读取C#中的.csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆