C# 是否有一种“正确”的方式来读取 CSV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1103495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:19:28  来源:igfitidea点击:

Is there a "proper" way to read CSV files

c#.netcsvoledb

提问by Gareth

Possible Duplicate:
CSV File Imports in .Net

可能的重复:
.Net 中的 CSV 文件导入

In .net, is there a standard library that should be used to read in csv files? All the samples on the web roll their own csv reader / parser, or use OleDb.

在 .net 中,是否有应该用于读取 csv 文件的标准库?网络上的所有示例都使用自己的 csv 阅读器/解析器,或者使用 OleDb。

It's not a problem using any of these solutions, I was just wondering if there is a generally accepted library (not that I can find), or any other "proper" way to do it?

使用这些解决方案中的任何一个都不是问题,我只是想知道是否有一个普遍接受的图书馆(不是我能找到的),或者任何其他“正确”的方法来做到这一点?

采纳答案by Marc Gravell

CsvReaderis a pretty good one... it isn't Microsoft, but it works very well, and is a lot faster than some of the alternatives (legacy OleDb etc).

CsvReader是一个相当不错的......它不是微软,但它工作得很好,并且比一些替代品(旧版 OleDb 等)快得多。

回答by MusiGenesis

I'm pretty sure you can read a CSV file into a DataTable with one line of code. Once it's in a DataTable, you can sort, filter, iterate etc.

我很确定您可以使用一行代码将 CSV 文件读入 DataTable。一旦它在数据表中,您就可以排序、过滤、迭代等。

This questionhas some examples for reading CSVs into DataTables.

这个问题有一些将 CSV 读入 DataTables 的例子。

回答by lavinio

One of the reasons that many people write their own is that CSV isn't quite so simple. For example:

许多人自己编写的原因之一是CSV 不是那么简单。例如:

  1. Does the first row contain field names, or not?
  2. Do you support dates? If, so, are they quoted, surrounded by # marks, in a certain day-month-year order?
  3. Does it support linefeeds that occur inside quoted text values? Or does that split the record?
  4. How do you escape a quote inside of a quoted string? Do you double the quote, or use a backslash or other escape character?
  5. What character encoding(s) are supported?
  6. How does it handle escaped control characters? &#XX; or \uXXXX or some other method?
  1. 第一行是否包含字段名称?
  2. 你支持日期吗?如果是这样,它们是否以特定的日-月-年顺序被引用,并被 # 标记包围?
  3. 它是否支持出现在引用文本值内的换行符?或者这是否打破了记录?
  4. 如何在带引号的字符串中转义引号?您是将引号加倍,还是使用反斜杠或其他转义字符?
  5. 支持哪些字符编码?
  6. 它如何处理转义的控制字符?&#XX; 或 \uXXXX 或其他一些方法?

These are someof the reasons people write their own parsers, because they're stuck reading files created with all these different settings. Or they write their own serializers, because the target system has a bunch of these idiosyncrasies.

这些是人们编写自己的解析器的一些原因,因为他们无法读取使用所有这些不同设置创建的文件。或者他们编写自己的序列化程序,因为目标系统有一堆这些特性

If you don't care about these issues, just use the most convenient library. But understand they are there.

如果你不关心这些问题,就用最方便的库。但要明白他们在那里。

回答by Gareth

After some more investigation, there is also this: http://www.filehelpers.com/

经过更多的调查,还有这个:http: //www.filehelpers.com/

It seems a full framework around reading files, and not just csv files.

它似乎是一个围绕读取文件的完整框架,而不仅仅是 csv 文件。

(note: just read stuff on the website, have not used it yet)

(注:刚看了网站上的东西,还没用过)

回答by aSkywalker

The VB namespace has a great TextFieldParserclass. I know, c# people don't like to reference a library from that 'basic' language, but it is quite good.

VB 命名空间有一个很棒的TextFieldParser类。我知道,c# 人不喜欢从“基本”语言中引用库,但它非常好。

It is located at Microsoft.VisualBasic.FileIO.TextFieldParser

它位于 Microsoft.VisualBasic.FileIO.TextFieldParser

I used to mess with OLEDB, creating column definition files etc - but find the TextFieldParser a very simple and handy tool for parsing any delimited files.

我曾经弄乱 OLEDB,创建列定义文件等 - 但发现 TextFieldParser 是一个非常简单且方便的工具,用于解析任何分隔文件。

回答by Kent Boogaart

KBCsvis another option, particularly if you require efficiency and the ability to work with massive CSV files.

KBCsv是另一种选择,特别是如果您需要效率和处理大量 CSV 文件的能力。

Disclosure: I wrote KBCsv, hence the "KB" ;)

披露:我写了 KBCsv,因此是“KB”;)

回答by Josh Close

Try CsvHelper(a library I maintain). It's also available via NuGet.

试试CsvHelper(我维护的一个库)。它也可以通过NuGet 获得

CsvHelper allows you to read your CSV file directly into your custom class.

CsvHelper 允许您将 CSV 文件直接读入自定义类。

var streamReader = // Create a reader to your CSV file.
var csvReader = new CsvReader( streamReader );
List<MyCustomType> myData = csvReader.GetRecords<MyCustomType>();

CsvReader will automatically figure out how to match the property names based on the header row (this is configurable). It uses compiled expression trees instead of reflection, so it's very fast.

CsvReader 将自动找出如何根据标题行匹配属性名称(这是可配置的)。它使用编译表达式树而不是反射,所以速度非常快。

It is also very extensible and configurable.

它也是非常可扩展和可配置的。