读取大型 csv 文件

发布于 2024-09-19 16:21:42 字数 54 浏览 3 评论 0原文

在 .NET 中读取大型 csv 文件的最高效方法是什么? 使用文件流?或者其他班级? 谢谢!

Which is the most performant way to read a large csv file in .NET?
Using FileStream? or another class?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吾性傲以野 2024-09-26 16:21:42

您可以使用StreamReaderFileInfo.OpenText

Dim file As New FileInfo("path\to\file")

Using reader As StreamReader = file.OpenText()
    While Not reader.EndOfStream
        Dim nextLine As String = reader.ReadLine()
        ProcessCsvLine(nextLine)
    End While
End Using

You can use the StreamReader returned by FileInfo.OpenText:

Dim file As New FileInfo("path\to\file")

Using reader As StreamReader = file.OpenText()
    While Not reader.EndOfStream
        Dim nextLine As String = reader.ReadLine()
        ProcessCsvLine(nextLine)
    End While
End Using
白芷 2024-09-26 16:21:42

如果您想将其全部读入内存,一个简单的 File.ReadAllText() 就可以了。

编辑:如果您的文件确实非常大,那么您可以使用StreamReader类,请参阅此处了解详细信息。这种方法有时是不可避免的,但出于风格原因应尽量避免。请参阅此处了解更深入的信息讨论。

If you want to read it all into memory, a simple File.ReadAllText() will do just fine.

EDIT: If your file is indeed very large, then you can use the StreamReader class, see here for details. This approach is sometimes inevitable but should mostly be avoided for style reasons. See here for a more in-depth discussion.

入怼 2024-09-26 16:21:42

最有效的方法是利用 LINQ 中的延迟执行。您可以创建一个简单的 Linq-To-Text 函数,该函数一次读取一行,对其进行处理,然后继续。这确实很有帮助,因为文件非常大。

我将停止使用 StreamReader 类的 ReadBlock 或 ReadBlock 或 ReadToEnd 方法,因为它们倾向于一次读取多行甚至文件中的整行。与一次读取一行相比,这最终会消耗更多的内存。

public static IEnumerable<string> Lines(this StreamReader source)
{
    String line;

    if (source == null)
        throw new ArgumentNullException("source");

    while ((line = source.ReadLine()) != null)
    {
        yield return line;
    }
}

请注意,该函数是 StreamReader 类的扩展方法。这意味着它可以按如下方式使用:

class Program
{
   static void Main(string[] args)
   {
       using(StreamReader streamReader = new StreamReader("TextFile.txt"))
       {
            var tokens = from line in streamReader.Lines()
            let items = line.Split(',')               
            select String.Format("{0}{1}{2}",
                items[1].PadRight(16),
                items[2].PadRight(16),
                items[3].PadRight(16));

       }
   }
}

The most efficient way of doing this is by taking advantage of deffered execution in LINQ. You can create a simple Linq-To-Text function that read one line at a time, work on it and then continue. This is really helpful since the file is really large.

I would desist from using the ReadBlock or ReadBlock or ReadToEnd methods of StreamReader class since they tend to read a number of lines at once or even the entire lines in the file. This ends up consuming more memory than if a line was read one at a time.

public static IEnumerable<string> Lines(this StreamReader source)
{
    String line;

    if (source == null)
        throw new ArgumentNullException("source");

    while ((line = source.ReadLine()) != null)
    {
        yield return line;
    }
}

Note that the function is an extension method of the StreamReader class. This means it can be used as follows:

class Program
{
   static void Main(string[] args)
   {
       using(StreamReader streamReader = new StreamReader("TextFile.txt"))
       {
            var tokens = from line in streamReader.Lines()
            let items = line.Split(',')               
            select String.Format("{0}{1}{2}",
                items[1].PadRight(16),
                items[2].PadRight(16),
                items[3].PadRight(16));

       }
   }
}
渔村楼浪 2024-09-26 16:21:42

我对这个库有很好的体验:

http://www.codeproject.com/KB /database/CsvReader.aspx

I had very good experience with this library:

http://www.codeproject.com/KB/database/CsvReader.aspx

节枝 2024-09-26 16:21:42

I am using this library for Csv reading. This is really nice to use

http://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文