如何从csv文件中取出无效的行?

问题描述

我使用 TinyCsvParser 作为工具将大型 csv 文件中的行映射到我的数据模型中。 所以,问题是我如何才能使用无效的行来为用户显示它?现在它可以与 Regex 一起使用,但我会用更具可读性的工具重新编写它。有可能吗?(现在我可以将 TinyCsvParser 更改为任何其他工具,这样可以快速处理大型 csv 文件并且具有可读的 api)。

现在看起来像: 型号:

public class Item
{
    public string Id { get; set; }
    public string Description { get; set; }
    public string Country { get; set; }
    public string Time { get; set; }
    public string ErrorDescription { get; set; }

    public override string ToString()
        => $"{Id } {Description } {Country} {Time} {ErrorDescription}";
}

解析器:

public class CsvItemmapping : CsvMapping<Item>
    {
        public CsvItemmapping()
            : base()
        {
            MapProperty(columnIndex: 0,x => x.Id);
            MapProperty(columnIndex: 1,x => x.Description );
            MapProperty(columnIndex: 2,x => x.Country);
            MapProperty(columnIndex: 3,x => x.Time);
            MapProperty(columnIndex: 4,x => x.ErrorDescription);
        }
    }

要解析的代码

// this is selects ONLY valid lines,that parser mapped to models.
// I want a flag like "Not valid" and display it at the time,when read this line             
    var csvParserOptions = new CsvParserOptions(skipHeader: true,fieldsSeparator: ',');
    var csvMapper = new CsvItemmapping();
    var csvParser = new CsvParser<Item>(csvParserOptions,csvMapper);
    
    const string path = @"...";
    var result = csvParser
        .ReadFromFile(path,Encoding.UTF8)
        .Select(x => x.Result)
        .ToList();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)