Lumenworks Fast CsvReader - Exception error reading tab delimit file due to quote character

青春壹個敷衍的年華 提交于 2019-12-05 09:03:36

There is a mssing closing bracket behind the StreamReader:

using (CsvReader csvReader = new CsvReader(new StreamReader(filePath), false, '\t', '"', '"', '#', LumenWorks.Framework.IO.Csv.ValueTrimmingOptions.All))
{
    int fieldCount = csvReader.FieldCount;
    while (csvReader.ReadNextRecord())
    {
        for (int i = 0; i < fieldCount; i++)
            Console.WriteLine("Column {0}: {1}", i + 1, csvReader[i]);
    }
}

I have tested it with your line above (forced tab as delimiter in the file) and it worked.

Output was:

Column 1: 12345
Column 2: 2013
Column 3: RAV4
Column 4: Sport
Column 5: Sport Utility
Column 6: 4D
Column 7: 2

Update, according your comment and the provided text-file:

This csv-reader enables to handle FillError and ParseError exceptions raised by invalid or corrupt data. So you handle them to get more informations and for logging purposes.

For example:

void csv_ParseError(object sender, ParseErrorEventArgs e)
{
    // if the error is that a field is missing, then skip to next line
    if (e.Error is MissingFieldCsvException)
    {
        //Log.Write(e.Error, "--MISSING FIELD ERROR OCCURRED!" + Environment.NewLine);
        e.Action = ParseErrorAction.AdvanceToNextLine;
    }
    else if (e.Error is MalformedCsvException)
    {
        //Log.Write(e.Error, "--MALFORMED CSV ERROR OCCURRED!" + Environment.NewLine);
        e.Action = ParseErrorAction.AdvanceToNextLine;
    }
    else
    {
        //Log.Write(e.Error, "--UNKNOWN PARSE ERROR OCCURRED!" + Environment.NewLine);
        e.Action = ParseErrorAction.AdvanceToNextLine;
    }
}

You need to listen to this event:

csvReader.MissingFieldAction = MissingFieldAction.ParseError;
csvReader.DefaultParseErrorAction = ParseErrorAction.RaiseEvent;
csvReader.ParseError += csv_ParseError;

I have recognized that it doesn't work to use " as quoting character with your text-file since some fields contain data like RAV4 "Sport" Sport Utility 4D. So the field itself contains the quoting character. Instead you don't need one at all since no fields are quoted. So don't provide one in the constructor or set it as '\0'. Then this runs without a problem:

using(var rd  = new StreamReader(filePath))
using (var csvReader = new CsvReader(rd, false, '\t', '\0', '\0', '#', ValueTrimmingOptions.All))
{
    csvReader.MissingFieldAction = MissingFieldAction.ParseError;
    csvReader.DefaultParseErrorAction = ParseErrorAction.RaiseEvent;
    csvReader.ParseError += csv_ParseError;
    csvReader.SkipEmptyLines = true;
    int fieldCount = csvReader.FieldCount;
    while (csvReader.ReadNextRecord())
    {
       var fields = new List<string>();
        for (int i = 0; i < fieldCount; i++)
        {
            fields.Add(csvReader[i]);
        }
        lines.Add(fields);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!