2017-08-25 17 views
0

我的目錄中有多個CSV文件,屬於三個不同的類別(文件A,文件B &文件C),具有不同的列結構/數據。我正在使用SmartXLS庫編寫控制檯應用程序,它讀取一個新文件(比如文件Z)並告訴我它屬於哪個類別(A/B/C)。如何使用C#將文件列表中的CSV文件驗證爲相應的類別?

我能夠從目錄中讀取多個文件,但無法寫入邏輯來驗證文件。請幫助我。

文件格式: 文件A:使用日期,產品名稱,用戶ID,使用的令牌。 文件B:原因,月份,調整日期,交易ID,令牌調整,
產品名稱,評論已添加。 文件C:使用日期,產品名稱,產品版本,用戶標識,機器名稱, 服務器名稱,使用的令牌,已用小時數。

功能:

public void ValidateData() 
     { 
      int count = 0; 

      Tokens = new List<Token>(); 

      var files = Directory.EnumerateFiles(@"C:\Projects", "*.csv"); 

      foreach (string file in files) 
      { 
       using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) 
       { 
        SmartXLS.WorkBook WB = new WorkBook(); 
        WB.readCSV(file); 

        DataTable dt = WB.ExportDataTable(); 
        string dtSTR; 
        DataRow dr; 

        for (int i = 1; i < dt.Rows.Count; i++) 
        { 
         dr = dt.Rows[i]; 

         try 
         { 
          dtSTR = dr[0].ToString(); 
          if (string.IsNullOrEmpty(dtSTR)) continue; 

          var tkn = new Token(); 
          tkn.Usagedate = ParseDateTime(dtSTR); 
          tkn.Product_name = dr[1].ToString(); 
          tkn.Product_Version = dr[2].ToString(); 
          tkn.Userid = dr[3].ToString(); 


          Tokens.Add(tkn); 
          count++; 

          Console.WriteLine("Read : " + count); 

          Console.WriteLine(" Reading : " + tkn.Usagedate + "," + tkn.Product_name + "," + tkn.Product_Version + "," + tkn.Userid); 

         } 

         catch (Exception ex) 
         { 

         } 

        } 
       } 

      } 
     } 
+0

_with不同的柱狀結構/ data._那些是什麼不同的結構? – Sach

+0

我試圖附上照片,但由於某種原因,它不允許我。 這裏是結構: 文件:使用日期,產品名稱,用戶ID,用令牌 文件B:原因,月,\t調整日期,交易ID,令牌調整,\t產品名稱,註釋添加 文件C:使用日期,產品名稱,產品版本,用戶標識,機器名稱,服務器名稱,使用的令牌,使用的小時數。 以上是三個文件的逗號分隔列標題@Sach – Rck7

+0

請用您的信息更新您的原始文章。 – Sach

回答

1

如果這些都是很簡單的.csv文件,你只需要對它們進行分類,你可以跳過幾乎所有的邏輯在using塊,並只把這些文件作爲簡單的文本,這可能會證明更有效。

你沒有提到這些格式有什麼不同,所以我只能猜測。每個類別是否有不同數量的列?如果是這樣,您可以選擇其中一行,使用String.Split來創建一個字符串數組(每列一個字符串),然後使用該數組的長度來確定有多少列。這將是這個樣子:

public void ValidateData() 
{ 
    var files = Directory.EnumerateFiles(@"C:\Projects", "*.csv"); 

    foreach (string file in files) 
    { 
     // get a string array with one string per row 
     string[] fileRows = File.ReadAllLines(file); 

     foreach (string row in fileRows) 
     { 
      // ignore blank lines 
      if (row.Trim() != "") 
      { 
       char[] splitOn = new char[] {','}; // if the csv files use comma as a delimiter 
       string[] columns = row.Split(splitOn); // this splits the row into one string per column 

       int numberOfCols = columns.Length; // this is how many columns you have 

       // use this info to determine the type of file. 
       if (numberOfCols == 4) 
       { 
        Debug.WriteLine("This is category A"); 
       } 
       else if (numberOfCols == 5) 
       { 
        Debug.WriteLine("This is category B"); 
       } 
       else if (numberOfCols == 7) 
       { 
        Debug.WriteLine("This is category C"); 
       } 
       else 
       { 
        Debug.WriteLine("Unexpected file type."); 
       } 

       break; // end the loop now - we've already done what we needed to do. 
      } 
     } 

    } 
} 

如果他們有相同的列數,但在第一列的信息是不同的,那麼你就可以改變你的if語句來檢查一些有關列[0]會例如,您說文件A在第一列中有一個日期,因此如果第一列包含日期,則可以使用DateTime.TryParse(columns[0], out var date)返回true;如果不包含日期,則返回false 。如果您對文件類型之間的差異更加具體,那麼提供特定幫助會更容易。

編輯:在上面的例子中,變量row代表第一行非空白的數據。如果您的CSV文件的第一行包含列標題,然後就檢查字符串才能看到該列標題就是:

public void ValidateData() 
{ 
    var files = Directory.EnumerateFiles(@"C:\Projects", "*.csv"); 

    foreach (string file in files) 
    { 
     // get a string array with one string per row 
     string[] fileRows = File.ReadAllLines(file); 

     foreach (string row in fileRows) 
     { 
      // ignore blank lines 
      if (row.Trim() != "") 
      { 
       char[] splitOn = new char[] { ',' }; // if the csv files use comma as a delimiter 
       string[] columns = row.Split(splitOn); // this splits the row into one string per column 

       // the third column (columns[2]) is the first that is different for all file types. 
       if (columns[2] == "User ID") 
       { 
        Debug.WriteLine("This is file A"); 
       } 
       else if (columns[2] == "Adjustment Date") 
       { 
        Debug.WriteLine("This is file B"); 
       } 
       else if (columns[2] == "Product Version") 
       { 
        Debug.WriteLine("This is file C"); 
       } 
       else 
       { 
        Debug.WriteLine("Unknown file type."); 
       } 

       break; // end the loop now - we've already done what we needed to do. 
      } 
     } 

    } 
} 
+0

我會在OP更新差異信息後發佈一個答案,但你打敗了我。這是非常多的。 +1沒有不錯的簡潔代碼。 – Sach

+0

謝謝。是的,我在發佈具體細節之前就已經開始了,但是這應該會讓OP非常接近基於他的澄清的解決方案。 –

+0

謝謝@ElementalPete。你能否建議我使用列名來驗證數據,而不是列數(列數)?因爲未來列數可能會改變。 – Rck7

相關問題