csv文件中的IntValue1是一个没有双引号的数字,但是当系统读取该行时,它会在开头添加双引号,并在行尾添加双引号,后跟分号以分隔行。 因此,IntValue1以双引号开头,系统不会将其识别为int ...请帮我修复此错误。
该模型:
[DelimitedRecord(",")] public class MyObject { private int _ID; public int ID { get { return _ID; } set { _ID = value; } } private DateTime _EventDate; public DateTime EventDate { get { return _EventDate; } set { _EventDate = value; } } private string _IPAddress; public string IPAddress { get { return _IPAddress; } set { _IPAddress = value; } } }用于读取和显示数据的代码:
private static void GetCsvData() { var engine = new FileHelperEngine<MyTypeObj>(); //The error occurs at this line: var result = engine.ReadFile("C:\\CsvFileName.csv"); //Code to display the Data }CSV文件如下所示:
发生错误时调试器返回的行的内容:
错误:
The IntValue1 in the csv file is a number without double quotes but when the system reads the row it adds double quotes at the beginning and also double quotes followed by semicolon at the end of the row to delimit the row. Because of this, the IntValue1 starts with a double quotes and the system doesn't recognize it as an int... Please help me to fix this error.
The model:
[DelimitedRecord(",")] public class MyObject { private int _ID; public int ID { get { return _ID; } set { _ID = value; } } private DateTime _EventDate; public DateTime EventDate { get { return _EventDate; } set { _EventDate = value; } } private string _IPAddress; public string IPAddress { get { return _IPAddress; } set { _IPAddress = value; } } }The code for reading & displaying the data:
private static void GetCsvData() { var engine = new FileHelperEngine<MyTypeObj>(); //The error occurs at this line: var result = engine.ReadFile("C:\\CsvFileName.csv"); //Code to display the Data }The CSV file looks like this:
Content of the row returned by the debugger when the error occurs:
Error:
最满意答案
问题#1:您的文件不是有效的CSV。 解决此问题的正确方法是更正导出文件的方式,因为它不导出有效的CSV文件。
这不是有效的CSV文件:
"ID,""EventDate"",""IPAddress"""; "1,""2013-01-19"",""11.81.11.00"""; "2,""2012-11-25"",""11.72.41.84"""; "3,""2011-12-27"",""15.80.";"3.36""" "4,""2014-08-17"",""17.72.";"9.24""" "5,""2012-01-30"",""90.94.27.108"""; "6,""2013-02-15"",""19.97.27.189""";Hack:您可以使用FileHelpers使用BeforeReadRecord事件导入无效的CSV文件。 有时您无法控制可能导入的CSV文件。
你的型号:
[IgnoreFirst] [DelimitedRecord(",")] public class MyTypeObj { [FieldConverter(ConverterKind.Int32)] public int ID; [FieldQuoted] [FieldConverter(ConverterKind.Date, "yyyy-mm-dd")] public DateTime EventDate; [FieldQuoted] public string IPAddress; }你的代码:
var engine = new FileHelperEngine<MyTypeObj>(); engine.BeforeReadRecord += (@base, args) => { // Convert this: "1,""2013-01-19"",""11.81.11.00"""; // to this: 1,"2013-01-19","11.81.11.00" args.RecordLine = args.RecordLine .Substring(1, args.RecordLine.Length - 3) .Replace("\"\"", "\""); }; var result = engine.ReadFile(@"C:\CsvFileName.csv");你仍然会遇到这些行的问题(为什么在值的中间有一个分号???):
"3,""2011-12-27"",""15.80.";"3.36""" "4,""2014-08-17"",""17.72.";"9.24"""您也可以在BeforeReadRecord修复这些问题,但最好弄清楚导出出了什么问题。
tl; dr修复文件的导出方式,以便获得有效的CSV。 修复导入应该只是一个绝对的最后手段hack。
Problem #1: Your file is not a valid CSV. The proper way to fix this problem would be to correct the way the file is being exported as it is not exporting a valid CSV file.
This is not a valid CSV file:
"ID,""EventDate"",""IPAddress"""; "1,""2013-01-19"",""11.81.11.00"""; "2,""2012-11-25"",""11.72.41.84"""; "3,""2011-12-27"",""15.80.";"3.36""" "4,""2014-08-17"",""17.72.";"9.24""" "5,""2012-01-30"",""90.94.27.108"""; "6,""2013-02-15"",""19.97.27.189""";The Hack: You can use FileHelpers to import invalid CSV files using the BeforeReadRecord event. Sometimes you are just not in control of what CSV file you may receive for importing.
Your Model:
[IgnoreFirst] [DelimitedRecord(",")] public class MyTypeObj { [FieldConverter(ConverterKind.Int32)] public int ID; [FieldQuoted] [FieldConverter(ConverterKind.Date, "yyyy-mm-dd")] public DateTime EventDate; [FieldQuoted] public string IPAddress; }Your code:
var engine = new FileHelperEngine<MyTypeObj>(); engine.BeforeReadRecord += (@base, args) => { // Convert this: "1,""2013-01-19"",""11.81.11.00"""; // to this: 1,"2013-01-19","11.81.11.00" args.RecordLine = args.RecordLine .Substring(1, args.RecordLine.Length - 3) .Replace("\"\"", "\""); }; var result = engine.ReadFile(@"C:\CsvFileName.csv");You will still have problems with these lines (why is there a semi-colon in the middle of the value???):
"3,""2011-12-27"",""15.80.";"3.36""" "4,""2014-08-17"",""17.72.";"9.24"""You could also fix these in BeforeReadRecord, but it would be best to figure out what is going wrong with the export.
tl;dr Fix how the file is exported so you get a VALID CSV. Fixing the import should only be an absolute last resort hack.
更多推荐
发布评论