读取大型 csv 文件
在 .NET 中读取大型 csv 文件的最高效方法是什么? 使用文件流?或者其他班级? 谢谢!
Which is the most performant way to read a large csv file in .NET?
Using FileStream? or another class?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
StreamReader
由FileInfo.OpenText
:
You can use the
StreamReader
returned byFileInfo.OpenText
:如果您想将其全部读入内存,一个简单的
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.最有效的方法是利用 LINQ 中的延迟执行。您可以创建一个简单的 Linq-To-Text 函数,该函数一次读取一行,对其进行处理,然后继续。这确实很有帮助,因为文件非常大。
我将停止使用 StreamReader 类的 ReadBlock 或 ReadBlock 或 ReadToEnd 方法,因为它们倾向于一次读取多行甚至文件中的整行。与一次读取一行相比,这最终会消耗更多的内存。
请注意,该函数是 StreamReader 类的扩展方法。这意味着它可以按如下方式使用:
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.
Note that the function is an extension method of the StreamReader class. This means it can be used as follows:
我对这个库有很好的体验:
http://www.codeproject.com/KB /database/CsvReader.aspx
I had very good experience with this library:
http://www.codeproject.com/KB/database/CsvReader.aspx
我正在使用这个库来读取 Csv。 真的很好
使用http ://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F
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