gpt4 book ai didi

csv - C# CSVHelper : After BadDataException Reader doesn't continue with next row

转载 作者:行者123 更新时间:2023-12-04 17:31:27 24 4
gpt4 key购买 nike

这是我发布的第一个问题,所以我希望我能解释得通俗易懂。

我正在读取 CSV 文件并想将正确的条目添加到用户对象列表中。当标志 ignoreBadCSVEntry 设置为 true 时,应忽略错误条目(导致 BadDataException),否则应停止读取文件并将列表设置为 null。

我见过将局部变量 isRecordBad 设置为 true 的解决方案(如提到的 here ),但我尝试仅在异常(exception)情况下处理它。

(对于格式错误的测试用例)CSV 看起来像这样:

"id";"deletedDate";"createDate";"otherAtr"
"000000";"2018-12-4T17:04:34.595+0200";"2019-06-05";"d2"
"WrongDelimiter";"2019-02-29T14:04:34.595+0200","2019-06-05";"d3"
"000001";"2018-12-31T18:04:34.595+0200";"2019-06-05";"d4"
"011111";"2019-01-01T18:04:34.595+0200";"2019-06-05";"d5"
[...]

我的代码如下所示:

try
{
Configuration csvConfig = new Configuration()
{
Delimiter = ";",
Quote = '"',

MissingFieldFound = (headerNames, index, context) => throw new CsvHelper.MissingFieldException(context, "Bad entry found at row " + context.RawRow + ": " + context.RawRecord.Replace("\"", "'")),
BadDataFound = context => throw new BadDataException(context, String.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")))
};

foreach (string currentFile in csvFiles)
{
using (var reader = new StreamReader(currentFile))
using (var csv = new CsvReader(reader, csvConfig))
{
var moreRecords = true;
while (moreRecords)
{
try
{
if (csv.Read())
{
var record = new User();
record = csv.GetRecord<User>();
InsertIntoUserList(record);
}
else
{
moreRecords = false;
}
}
catch (Exception e)
{
if (e.GetType() == typeof(FormatException) ||
e.GetType() == typeof(BadDataException) ||
e.InnerException != null && e.InnerException.GetType() == typeof(MissingFieldException))
{
if (!ignoreBadCSVEntry)
{
throw;
}
_logger.LogWarning(e.Message);
}
}
}
_logger.LogDebug("Read List with {0} elements from file(s) in directory {1}.", this.UsersList.Count, sourceDir);
}
}
}
catch (Exception e)
{
_logger.LogError("Error while iterating over CSV files in directory {0} ", sourceDir);
_logger.LogError(e.Message);

this.List = null;
throw;
}

因此内部 catch block 中的第一条错误消息如下所示:

BadDataFound: Bad entry found at field 2019-02-29T14:04:34.595+0200,"2019-06-05", row 3: 'WrongDelimiter';'2019-02-29T14:04:34.595+0200','2019-06-05';

并且不会继续阅读下一行,而是会出现更多 BadDataExceptions。以下错误消息将继续显示错误字段,并在其后添加既不带分隔符也不带引号的下一个字段。喜欢:

BadDataFound: Bad entry found at field 2019-02-29T14:04:34.595+0200,"2019-06-05"d3, row 4: 'd3'

BadDataFound: Bad entry found at field 2019-02-29T14:04:34.595+0200,"2019-06-05"d3000001, row 5: '000001';

我在这里做错了什么?为什么读者不继续阅读下一行而是从错误的条目开始?

提前致谢!

最佳答案

BadDataFound 中抛出会中断 CsvHelper 对该行的处理(就像移动到下一行),这就是它在发现错误数据后继续处理下一项的原因。您需要将抛出或不抛出的逻辑放在 BadDataFound 中。

BadDataFound = context => {
if (!ignoreBadCSVEntry)
{
throw new BadDataException(context, string.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")));
}
_logger.LogWarning(string.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")));
}

关于csv - C# CSVHelper : After BadDataException Reader doesn't continue with next row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59272831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com