有效的csv中的CSVHelper BadDataFound [英] CSVHelper BadDataFound in a valid csv

查看:199
本文介绍了有效的csv中的CSVHelper BadDataFound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的客户开始报告从CSV文件导入数据的错误.看到csv文件后,我们决定从自定义CSV解析器切换到CSVHelper,但是CSV Helper无法读取某些有效的CSV文件.

Our customer started reporting bugs with importing data from CSV file. After seeing the csv file, we decided to switch from custom CSV parser to CSVHelper, but the CSV Helper can't read some valid CSV files.

用户可以将任何csv文件加载到我们的应用程序中,因此我们不能使用任何类映射器.我们使用csv.Parser.Read读取string [] dataRows.我们无法更改此csv文件的生成方式,该文件是由另一家公司生成的,并且我们无法说服他们在此文件为有效格式时更改其生成方式.

The users are able to load any csv file into our application, so we can't use any class mapper. We use csv.Parser.Read to read string[] dataRows. We can't change a way how this csv file is generated, it is generated by another company and we can't convince them to change the generation when this file is in a valid format.

如果我们使用BadDataFound处理程序,则context.RawRecord为:

If we youse BadDataFound handler, the context.RawRecord is:

"1000084;SMRSTOVACI TRUBICE PBF 12,7/6,4 (1/2\") H;"

csv文件中的数据行是:

the data row in csv file is:

1000084;SMRSTOVACI TRUBICE PBF 12,7/6,4 (1/2") H;;;ks;21,59;26,46;21.00;;;8591735015183;8591735015183;Technik;Kabelový spojovací materiál;Označování, smršťování, izolace;Bužírky, smršťovačky;

根据RFC 4180,这应该是有效的csv文件.

This should be a valid csv file by RFC 4180.

代码是:

using (var reader = new StreamReader(filePath, Encoding.Default))
{
    using (var csv = new CsvReader(reader))
    {
        csv.Read();
        csv.ReadHeader();

        List<string> badRecord = new List<string>();
        csv.Configuration.BadDataFound = context => badRecord.Add(context.RawRecord);

        header = csv.Context.HeaderRecord.ToList();

        while (true)
        {
            var dataRow = csv.Parser.Read();
            if (dataRow == null)
            {
                break;
            }

            data.Add(dataRow);
        }
    }
}

您能帮我配置CSVHelper以便将该行加载到字符串[]吗?还是可以建议使用其他解析方法来做到这一点?

Can you help me to configure CSVHelper to be able to load this row to string[]? Or can you suggest different parse which will be able to do that?

谢谢

推荐答案

我认为是引起问题的原因是该行中间的引号.尝试将配置设置为忽略引号.

I believe it is the quote in the middle of the row that is causing the issue. Try setting the configuration to ignore quotes.

using (var reader = new StreamReader(filePath, Encoding.Default))
{
    using (var csv = new CsvReader(reader))
    {
        csv.Configuration.Delimiter = ";";
        csv.Configuration.IgnoreQuotes = true;

        csv.Read();
        csv.ReadHeader();

        List<string> badRecord = new List<string>();
        csv.Configuration.BadDataFound = context => badRecord.Add(context.RawRecord);

        header = csv.Context.HeaderRecord.ToList();

        while (true)
        {
            var dataRow = csv.Parser.Read();
            if (dataRow == null)
            {
                break;
            }

            data.Add(dataRow);
        }
    }
}

这篇关于有效的csv中的CSVHelper BadDataFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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