2016-09-14 120 views
0

我正在使用此库:CSV Reader但问题是.csv文件的畸形。CSV文件每行不同数量的记录(CSV阅读器)

实施例:

,UDEQPT,,PROMIS,,,,,,,,,,,,,,,,,,,,,,,,,10:20:15,27-Dec-2015, 
,UDEQPT,,DELAY,,,,,,,,,am24134_1_drift,am24134.1_drift,229,19,,,3176.00,164.78,,,,,,5, 1.00,1,06:16:16,15-Jun-2016,,,,,,, 
,UDEQPT,,DELAY,,,,,,,,,am24134_1_drift,am24134.1_drift,345,25,,,131.68,216.71,,,,,,6, 1.00,1,06:28:23,15-Jun-2016,,,,,,, 
,UDEQPT,,DELAY,,,,,,,,,am24134_1_drift,am24134.1_drift,346,25,,,170.18,210.93,,,,,,7, 1.00,1,06:31:18,15-Jun-2016,,,,,,, 
,UDEQPT,,DELAY,,,,,,,,,am24134_1_drift,am24134.1_drift,376,27,,,295.83,212.99,,,,,,8, 1.00,1,06:38:47,15-Jun-2016,,,,,,, 
,UDEQPT,,ENDLOT,,,,def,def,def,def,,am24134_1_drift,am24134.1_drift,385,27,,,1214.13,213.82, 3.48, 3.11, 1.64, 25.96,1,8, 1.00,1,06:59:46,15-Jun-2016,,4395.91,1465945186,,def,0,1,385, 3.48,357,385, 92.9,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 

的列数是54,所以如果数据在一个行的数量小于所述固定数目的列,它给你错误。在上面的示例中,第一行仅在索引30之前。您如何正确处理此问题?

这里是我的代码:

using (var path = File.OpenRead(e.FullPath)) 
      { 
       using (var csv = new CachedCsvReader(new StreamReader(path), false)) 
       { 
        csv.Columns = new List<Column> 
        { 
         new Column { Name = "Delay_Code", Type = typeof(string) }, 
         new Column { Name = "PROMIS_Code", Type = typeof(string) }, 
         new Column { Name = "Tester_Mode", Type = typeof(string) }, 
         new Column { Name = "Event_Name", Type = typeof(string) }, 
         new Column { Name = "Test_Program", Type = typeof(string) }, 
         new Column { Name = "Temperature", Type = typeof(int?) }, 
         new Column { Name = "Lot_Size", Type = typeof(int?) }, 
         new Column { Name = "Part_Name", Type = typeof(string) }, 
         new Column { Name = "Procedure_Name", Type = typeof(string) }, 
         new Column { Name = "Handler_Id", Type = typeof(string) }, 
         new Column { Name = "Perf_Board", Type = typeof(string) }, 
         new Column { Name = "Sys_Part_Type", Type = typeof(string) }, 
         new Column { Name = "Lot_Id", Type = typeof(string) }, 
         new Column { Name = "Stage", Type = typeof(string) }, 
         new Column { Name = "Parts_Tested", Type = typeof(int?) }, 
         new Column { Name = "Parts_Failed", Type = typeof(int?) }, 
         new Column { Name = "Reprobes", Type = typeof(int?) }, 
         new Column { Name = "Successful_Reprobes", Type = typeof(int?) }, 
         new Column { Name = "Delay_Time", Type = typeof(float?) }, 
         new Column { Name = "UPH", Type = typeof(float?) }, 
         new Column { Name = "Test_Time_Pass", Type = typeof(float?) }, 
         new Column { Name = "Test_Time_Fail", Type = typeof(float?) }, 
         new Column { Name = "Avg_Index_Time", Type = typeof(float?) }, 
         new Column { Name = "Delays_30Sec_Avg", Type = typeof(float?) }, 
         new Column { Name = "Delays_30Sec_Count", Type = typeof(int?) }, 
         new Column { Name = "Delays_Count", Type = typeof(int?) }, 
         new Column { Name = "Avg_Num_Sites", Type = typeof(float?) }, 
         new Column { Name = "Active_Sites", Type = typeof(float?) }, 
         new Column { Name = "Hour_Min_Sec", Type = typeof(string) }, 
         new Column { Name = "Day_Month_Year", Type = typeof(string) }, 
         new Column { Name = "User_Name", Type = typeof(string) }, 
         new Column { Name = "Delays_Total_Duration", Type = typeof(float?) }, 
         new Column { Name = "Duration_Since_Last_End_Lot", Type = typeof(float?) }, 
         new Column { Name = "Start_Lot_Time_Data_Entry", Type = typeof(float?) }, 
         new Column { Name = "Employee_Id", Type = typeof(string) }, 
         new Column { Name = "Valid_Flag", Type = typeof(int?) }, 
         new Column { Name = "Sample_Rate", Type = typeof(int?) }, 
         new Column { Name = "Handler_Cycles", Type = typeof(int?) }, 
         new Column { Name = "Site_1_Only_Pass_Only_Avg_Test_Time", Type = typeof(float?) }, 
         new Column { Name = "Site_1_Only_Pass_Only_Count", Type = typeof(int?) }, 
         new Column { Name = "Site_1_Count", Type = typeof(int?) }, 
         new Column { Name = "Site_1_Yield", Type = typeof(float?) }, 
         new Column { Name = "Site_2_Only_Pass_Only_Avg_Test_Time", Type = typeof(float?) }, 
         new Column { Name = "Site_2_Only_Pass_Only_Count", Type = typeof(int?) }, 
         new Column { Name = "Site_2_Count", Type = typeof(int?) }, 
         new Column { Name = "Site_2_Yield", Type = typeof(float?) }, 
         new Column { Name = "Site_3_Only_Pass_Only_Avg_Test_Time", Type = typeof(float?) }, 
         new Column { Name = "Site_3_Only_Pass_Only_Count", Type = typeof(int?) }, 
         new Column { Name = "Site_3_Count", Type = typeof(int?) }, 
         new Column { Name = "Site_3_Yield", Type = typeof(float?) }, 
         new Column { Name = "Site_4_Only_Pass_Only_Avg_Test_Time", Type = typeof(float?) }, 
         new Column { Name = "Site_4_Only_Pass_Only_Count", Type = typeof(int?) }, 
         new Column { Name = "Site_4_Count", Type = typeof(int?) }, 
         new Column { Name = "Site_4_Yield", Type = typeof(int?) }, 
        }; 

        csv.MissingFieldAction = MissingFieldAction.ReplaceByNull; 
        csv.SkipEmptyLines = false; 
        csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent; 
        csv.ParseError += Csv_ParseError; 

        while (csv.ReadNextRecord()) 
        { 
         for (int i = 0; i < 54; i++) 
          Console.Write(string.Format(i + ". {0} |", string.IsNullOrEmpty(csv[i]) ? "MISSING" : csv[i])); 
         Console.WriteLine(); 
        } 

处理丢失的领域:

private static void Csv_ParseError(object sender, ParseErrorEventArgs e) 
     { 
      if (e.Error is MissingFieldCsvException) 
      { 
       e.Action = ParseErrorAction.AdvanceToNextLine; 
      } 
     } 
+0

如何处理这应该是一个商业逻辑,这意味着每个案例。有些人会忽略整条线,有些人可能会拒绝整个文件。也许你可以告诉我们你想如何处理,并让我们看看我们可以提供什么帮助 – Prisoner

+0

如果你想拥有这样一个自定义的文件格式,你需要自己阅读并解析它们。 –

+0

那么,你现在的方法的实际问题是什么? – grek40

回答

0

最后,我没有使用任何CSV库。我只是做了这个Variable Column CSV file processing C#,它的作品像魅力。我还创建了一个DataTable,然后使用SQLBulkCopy将其写入服务器。

0

你应该换你与一个循环,如果(csv.count == 54)来检测,如果该行是有效或没有进入循环,之后你可以指定每个字段的错误,如Delay_Code和一个专用的if,都取决于你想要的逻辑。

+0

我希望有一种方法可以获取读者正在阅读的当前行的数量。就像在完成第一行之后一样,有一种方法可以计算下一行。 –

+0

我想你可以使用csv.Count和csv.FieldCount就像这个例子:https://social.msdn.microsoft.com/Forums/windows/en-US/b29d3f03-06c1-48cf-a011-9ef66ba386e6/parsed- csv-into-datagridview-but-i-no-idea-how-to-save-into-mysql?forum = winformsdatacontrols –

+0

这不起作用,因为csv.Count返回1。 –