[英]Lumenworks Fast CsvReader - Exception error reading tab delimit file due to quote character
I'm using Lumenworks Fast CsvReader and the exception error occurred while reading the Kelley Blue Book's file: 我正在使用Lumenworks Fast CsvReader,并且在阅读Kelley Blue Book的文件时发生了异常错误:
The CSV appears to be corrupt near record '1281' field '5 at position '1169' 在位于'1169'的记录'1281'字段'5附近,CSV似乎已损坏
The file is tab delimited. 该文件以制表符分隔。 In there I found double quotes was used but I don't see how to escape it and resume normally because it's tab delimited data. 在那里我发现使用双引号,但我没有看到如何逃避它并正常恢复,因为它是制表符分隔数据。
--Characters in Text File--
12345 2013 RAV4 "Sport" Sport Utility 4D 2
--Source Code--
using(CsvReader csvReader = new CsvReader(new StreamReader(filePath), false, '\t', '"', '"', '#', LumenWorks.Framework.IO.Csv.ValueTrimmingOptions.QuotedOnly))
{
}
I tried a number of different CsvReader setting with no luck. 我尝试了许多不同的CsvReader设置而没有运气。 What do you use that works great? 你用的是什么效果很好? I do not have that much trouble with comma delimited files. 我用逗号分隔文件没有那么多麻烦。
There is a mssing closing bracket behind the StreamReader
: StreamReader
后面有一个mssing结束括号:
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. 此csv-reader可以处理由无效或损坏的数据引发的FillError
和ParseError
异常。 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: 我已经认识到使用"
作为引用字符与文本文件不起作用是不行的,因为某些字段包含RAV4 "Sport" Sport Utility 4D
。因此字段本身包含引号字符。相反,您不需要一个因为没有引用任何字段。所以不要在构造函数中提供一个或将其设置为'\\0'
。然后运行没有问题:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.