什么是在C#中异步解析CSV文件的最佳方法 [英] Whats the best way of asynchronous parsing a CSV file in c#

查看:200
本文介绍了什么是在C#中异步解析CSV文件的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须解析大型CSV文件并提高吞吐量,我正在使用异步并等待.我目前的方法是逐行读取文件并执行自己的解析:

I have to parse big CSV files and to improve throughput I am using async and await. My current approach is read the file line by line and perform my own parsing:

using (var streamReader = File.OpenText(fileName))
{
  string line;
  while ((line = await streamReader.ReadLineAsync()) != null)
  {
    // Parse line ...
  }
}

但是,正确解析CSV文件并不容易,例如当字符串包含逗号等时.

However, proper parsing of CSV files is not easy e.g. when strings contain commas etc.

我一直无法找到既性能良好又支持异步和等待的CSV解析器.如何在不从头开始编写解析器的情况下,使用异步I/O有效地解析CSV文件?

I have been unable to find a CSV parser that both performs well and supports async and await. How can I efficiently parse a CSV file using asynchronous I/O without writing the parser from scratch?

推荐答案

您可以看看非常有用且易于使用的 CsvHelper库(也可以作为 NuGet包使用).这甚至包括内置的映射功能,可将CSV记录映射到对象.

You could have a look at the very useful and easy to use CsvHelper library (also available as a NuGet package). This even includes built-in mapping functionality to map CSV records to objects.

CSV Helper 用于读取和写入CSV文件的.NET库.极其快速,灵活且易于使用.支持阅读和写作 自定义类对象.

CSV Helper A .NET library for reading and writing CSV files. Extremely fast, flexible and easy to use. Supports reading and writing of custom class objects.

它不是异步的(如async/await所示),但是如注释中所建议,您可以围绕它创建自己的异步包装器.

It isn't asynchronous (as in async/await), but as suggested in the comments, you could create your own asynchronous wrapper around it.

这篇关于什么是在C#中异步解析CSV文件的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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