gpt4 book ai didi

c# - CsvHelper 未正确解析

转载 作者:行者123 更新时间:2023-11-30 12:41:47 24 4
gpt4 key购买 nike

我有以下 CSV。

Org Defined ID,Username,FirstName,LastName,Attempt #,Attempt Start,Attempt End,Section #,Q #,Q Type,Q Title,Q Text,Bonus?,Difficulty,Answer,Answer Match,Score,Out Of
,testomalley,Test,O'Malley,1,2/3/2016 15:24,2/3/2016 15:28,,1,LA,Q(1) 1- 5 Part 1,"Scenario 1 for Questions 1 through 5. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Part 1 - Sed ut perspiciatis unde omnis iste natus error sit voluptatem.",FALSE,1,Here is my answer o grader. Isn't it brilliant?,,0,2

现在这是我要将其转换为的对象:

public class Exam
{
public int? OD_ID { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int AttemptNo { get; set; }
public DateTime AttemptStart { get; set; }
public DateTime AttemptEnd { get; set; }
public int? SectionNo { get; set; }
public int QuestionNo { get; set; }
public string QuestionType { get; set; }
public string QuestionTitle { get; set; }
public string Questiontext { get; set; }
public string Bonus { get; set; }
public int Difficulty { get; set; }
public string Answer { get; set; }
public string AnswerMatch { get; set; }
public int Score { get; set; }
public int OutOf { get; set; }
}

我定义了这张 map :

public sealed class ExamMap : CsvHelper.Configuration.CsvClassMap<Exam>
{
public ExamMap()
{
Map(m => m.OD_ID).Name("Org Defined ID");
Map(m => m.Username).Name("Username");
Map(m => m.FirstName).Name("FirstName");
Map(m => m.LastName).Name("LastName");
Map(m => m.AttemptNo).Name("Attempt #");
Map(m => m.AttemptStart).Name("Attempt Start");
Map(m => m.AttemptEnd).Name("Attempt End");
Map(m => m.SectionNo).Name("Section #");
Map(m => m.QuestionNo).Name("Q #");
Map(m => m.QuestionType).Name("Q Type");
Map(m => m.QuestionTitle).Name("Q Title");
Map(m => m.Questiontext).Name("Q Text");
Map(m => m.Bonus).Name("Bonus?").TypeConverterOption(true, "TRUE").TypeConverterOption(false, "FALSE");
Map(m => m.Difficulty).Name("Difficulty");
Map(m => m.Answer).Name("Answer");
Map(m => m.AnswerMatch).Name("Answer Match");
Map(m => m.Score).Name("Score");
Map(m => m.OutOf).Name("Out Of");
}
}

我的 DateTime 字段的这个 TypeConversion 选项:

var options = new CsvHelper.TypeConversion.TypeConverterOptions
{
// 2/3/2016 15:24
Format = "MM/dd/yyyy HH:nn", // also tried "g" with no success.
};

这是解析 CSV 文件的代码:

using (StreamReader sr = new StreamReader(fileName))
{
using (CsvReader csvread = new CsvReader(sr))
{
csvread.Configuration.WillThrowOnMissingField = false;
csvread.Configuration.HasHeaderRecord = true;
CsvHelper.TypeConversion.TypeConverterOptionsFactory.AddOptions<DateTime>(options);

List<Exam> Exams = csvread.GetRecords<Exam>().ToList();
foreach (var rec in Exams)
{
// Each record will be fetched and printed on the screen
// checking values of rec here...
}
}
}

这是它被解析的方式: Values

为什么它没有正确解析 DateTime 字段以及为什么没有填充这些属性?

问题编号
问题类型
题目
问题文本
答案匹配
尝试否
超出

最佳答案

你可以像这样添加一个DateTime解析的方法(你现在甚至可以设置断点并修改选项):

Map(t => t.AttemptStart).Name("Attempt Start")
.ConvertUsing(new Func<CsvHelper.ICsvReaderRow, DateTime>(r =>
{
DateTime dateTimeValue;
if (DateTime.TryParse(r["Attempt Start"], null,
System.Globalization.DateTimeStyles.AllowWhiteSpaces, out dateTimeValue))
{
return dateTimeValue;
}

return default(DateTime);
}));

缺少的字段可能是基于 DateTime 解析问题的错误。乍一看它看起来是正确的。

关于c# - CsvHelper 未正确解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36460056/

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