解析CSV文件在C#中,有头 [英] Parsing CSV files in C#, with header

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

问题描述

有一个缺省的/官方/推荐解析在C#中的CSV文件的方法吗?我不希望推出自己的解析器。

Is there a default/official/recommended way to parse CSV files in C#? I don't want to roll my own parser.

另外,我已经看到了使用ODBC / OLE DB通过文本驱动程序来读取CSV人的情况下,和很多人鼓励这种由于它的缺点。这些是什么缺点?

Also, I've seen instances of people using ODBC/OLE DB to read CSV via the Text driver, and a lot of people discourage this due to its "drawbacks." What are these drawbacks?

在理想情况下,我正在寻找一种方式,通过它我可以通过列名读取CSV,使用第一个记录作为标题/字段名。有些给定的答案是正确的,但工作文件基本上反序列化为类。

Ideally, I'm looking for a way through which I can read the CSV by column name, using the first record as the header / field names. Some of the answers given are correct but work to basically deserialize the file into classes.

推荐答案

让库处理所有的细枝末节为您服务! : - )

Let a library handle all the nitty-gritty details for you! :-)

查看 FileHelpers 并保持干燥 - 不要重复自己 - 没有必要重新发明轮子一个gazillionth时间...

Check out FileHelpers and stay DRY - Don't Repeat Yourself - no need to re-invent the wheel a gazillionth time....

您基本上只需要定义数据的形状 - 在CSV您的个人行领域 - 通过公共类的方法(和想好做出来的属性一样的默认值,NULL值替换等等),指向一个文件FileHelpers引擎,和宾果 - 你回来从该文件中的所有条目。一个简单的操作 - 伟大的业绩。

You basically just need to define that shape of your data - the fields in your individual line in the CSV - by means of a public class (and so well-thought out attributes like default values, replacements for NULL values and so forth), point the FileHelpers engine at a file, and bingo - you get back all the entries from that file. One simple operation - great performance!

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

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