如何使用CsvReader清理CSV输入?

时间:2021-07-05 15:41:23

I'm using the CsvReader library in my Windows Forms application, which is coded in horribly messy VB (I've recently taken over the project.)

我在我的Windows窗体应用程序中使用了CsvReader库,该应用程序以可怕的混乱VB编码(我最近接管了该项目。)

I'm currently capable of reading semi-colon separated files without quoting, but I'm having a problem: most of the input has quoted fields, but includes unescaped quote characters within the fields.

我目前能够在不引用的情况下读取分号分隔文件,但我遇到了问题:大多数输入都有引用字段,但在字段中包含未转义的引号字符。

I cannot change the input, so I must tackle it somehow. One solution would be to entirely disable quoting -- but I'm not quite sure how to do that...

我不能改变输入,所以我必须以某种方式解决它。一种解决方案是完全禁用引用 - 但我不太确定如何做到这一点......

Any help would be appreciated!

任何帮助,将不胜感激!

3 个解决方案

#1


2  

I've only played a little with this library LINQtoCSV, but I think it may help with quoted fields.

我只使用这个库LINQtoCSV玩了一点,但我认为它可能有助于引用字段。

#2


1  

Check out FileHelpers.

查看FileHelpers。

#3


0  

How generic do you need it to be? It is quite simple to write your own CSV parser that reads the data into a DataTable.

您需要它的通用性如何?编写自己的CSV解析器可以非常简单地将数据读入DataTable。

In some cases parsing the csv file can be as simple as doing a File.ReadAllLines(filename) and then for each line doing a string.Split using a semicolon as the separator to get the columns. Your code will probably be more advanced than that, but if you have specific needs writing it yourself is probably a good idea.

在某些情况下,解析csv文件可以像执行File.ReadAllLines(filename)一样简单,然后对每个执行string.Split的行使用分号作为分隔符来获取列。您的代码可能会比这更高级,但如果您有特定需求,那么自己编写代码可能是个好主意。

#1


2  

I've only played a little with this library LINQtoCSV, but I think it may help with quoted fields.

我只使用这个库LINQtoCSV玩了一点,但我认为它可能有助于引用字段。

#2


1  

Check out FileHelpers.

查看FileHelpers。

#3


0  

How generic do you need it to be? It is quite simple to write your own CSV parser that reads the data into a DataTable.

您需要它的通用性如何?编写自己的CSV解析器可以非常简单地将数据读入DataTable。

In some cases parsing the csv file can be as simple as doing a File.ReadAllLines(filename) and then for each line doing a string.Split using a semicolon as the separator to get the columns. Your code will probably be more advanced than that, but if you have specific needs writing it yourself is probably a good idea.

在某些情况下,解析csv文件可以像执行File.ReadAllLines(filename)一样简单,然后对每个执行string.Split的行使用分号作为分隔符来获取列。您的代码可能会比这更高级,但如果您有特定需求,那么自己编写代码可能是个好主意。