2017-01-31 26 views
0

此代碼通過讀取文本文件內容,處理其中的數據並將輸出顯示在文本框中起作用。在C#ASP.Net中顯示來自文本文件的空值或零值數據

using (StreamReader stRead = new StreamReader(FileUpload1.PostedFile.InputStream)) 
      { 
       string filenameDate = FileUpload1.FileName.Substring(15, 2); 
       Dictionary<string, int> dMyobject = new Dictionary<string, int>(); 

       while (!stRead.EndOfStream) 
       { 
        var readedLine = stRead.ReadLine(); 

        if (!string.IsNullOrWhiteSpace(readedLine)) 
        { 

         //int readedLineTime = Convert.ToInt32(readedLine.Substring(09, 02)); 
         string sDate = readedLine.Substring(0, 11); 

         MatchCollection collection = Regex.Matches(readedLine, @"D;"); 
         countedChars = collection.Count; 




         if (!dMyobject.Keys.Contains(sDate)) 
         { 
          dMyobject.Add(sDate, collection.Count); 
         } 
         else 
         { 
          dMyobject[sDate] = dMyobject[sDate] + collection.Count; 
         } 


        } 
        textfileContent += readedLine + Environment.NewLine; 
        i++; 
       } 
       txtContent.Text = textfileContent; 
       lblLineCount.Text = i.ToString(); 
       //Label1.Text = this.TextBox1.Text.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Length.ToString(); 
       lblFileDate.Text = filenameDate; 




       foreach (var item in dMyobject) 
       { 
        textfileOutput += (item.Key + " " + item.Value) + Environment.NewLine; 
        // textfileOutput += (item.Value) + Environment.NewLine; 
       } 
       txtOutput.Text = textfileOutput; 



      } 

textfile中的每一行都包含一個日期和一系列模式。此代碼讀取由日期分隔的每行並計算模式的總髮生次數,在本例中,計數模式「D」。並總結其日期總數(相同日期的行應彙總)。例如

enter image description here

運行代碼會給這個電流輸出

2016-12-01 - 7 
2016-12-02 - 9 
2016-12-03 - 5 
2016-12-05 - 1 

我的目標和期望輸出的輸出應包括與NULL值的日期。在我的例子,有沒有2016年12月4日的記錄,預計**輸出應該仍然顯示日期和顯示0或空白,如:**

2016-12-01 - 7 
2016-12-02 - 9 
2016-12-03 - 5 
2016-12-04 - 0 
2016-12-05 - 1 

回答

1

這將做到這一點。

using (StreamReader stRead = new StreamReader(@"c:\test.txt")) 
{ 
    string filenameDate = "test"; 
    string textfileContent = string.Empty; 
    int i = 0; 
    string textfileOutput = string.Empty; 
    Dictionary<string, int> dMyobject = new Dictionary<string, int>(); 

    while (!stRead.EndOfStream) 
    { 
     var readedLine = stRead.ReadLine(); 

     if (!string.IsNullOrWhiteSpace(readedLine)) 
     { 
      string sDate = readedLine.Substring(0, 11).Trim(); 

      MatchCollection collection = Regex.Matches(readedLine, @"D;"); 
      if (!dMyobject.Keys.Contains(sDate)) 
      { 
       dMyobject.Add(sDate, collection.Count); 
      } 
      else 
      { 
       dMyobject[sDate] = dMyobject[sDate] + collection.Count; 
      } 


     } 
     textfileContent += readedLine + Environment.NewLine; 
     i++; 
    } 

    var date = DateTime.Parse(dMyobject.First().Key); 
    var beginOfMonth = new DateTime(date.Year, date.Month, 1); 
    var days = new Dictionary<string, int>(); 
    for (var x = 0; x < DateTime.DaysInMonth(date.Year, date.Month); x++) 
    { 
     days.Add(beginOfMonth.AddDays(x).ToString("yyyy-MM-dd"), 0); 
    } 

    foreach (var item in days) 
    { 
     textfileOutput += (dMyobject.ContainsKey(item.Key) ? (item.Key + " " + dMyobject[item.Key]) : (item.Key + " 0")) + Environment.NewLine; 
    } 

} 

所以代碼的最後位開始創建由從Dictionary採摘日期和迭代,以該月的最後一天所需的日期範圍。然後它會檢查您的匹配項並根據日期範圍進行計數,如果匹配,則使用更新的計數而不是0

+0

你是對的,道歉,請看我編輯的問題@johanP – rickyProgrammer

+0

更新了我的回答 – JohanP

+0

感謝這個,但唯一可能缺少的是如果例如,第一個日期爲空,dec 1爲空,它不會顯示它 – rickyProgrammer

1

上傳文件中,有2016 -12-04行,你只需編輯你Regex獲得所有字符並計算總分數。

修復1:檢查連續時間。

using (StreamReader stRead = new StreamReader(FileUpload1.PostedFile.InputStream)) 
       { 
        string filenameDate = FileUpload1.FileName.Substring(15, 2); 
        SortedDictionary<string, int> dMyobject = new SortedDictionary<string, int>(); //this is a dictionary sorted by key 
      DateTime? startDatetime = null, endDatetime = null;//got mininum and maxinum dates, at late will be check the continuous time 

        while (!stRead.EndOfStream) 
        { 
         var readedLine = stRead.ReadLine(); 

         if (!string.IsNullOrWhiteSpace(readedLine)) 
         { 

          string sDate = readedLine.Substring(0, 11).Trim(); 
        DateTime date; 
        if (DateTime.TryParse(sDate, out date)) 
        { 
         if (startDatetime.HasValue == false) 
          startDatetime = date; 
         endDatetime = date; 
//got start date and end date 
//if date does not from big to small 
//here need compare bwteen date and endDatetime 
        } 


       MatchCollection collection = Regex.Matches(readedLine, "(?<c>[A-Z]+);"); 

          if (!dMyobject.Keys.Contains(sDate)) 
          { 
           dMyobject.Add(sDate, GetTotal(collection)); 
          } 
          else 
          { 
           dMyobject[sDate] = dMyobject[sDate] + GetTotal(collection); 
          } 


         } 
         textfileContent += readedLine + Environment.NewLine; 
         i++; 
        } 

    //here is check the continuous time 
      if (startDatetime.HasValue && endDatetime.HasValue) 
      { 
       for (var dt = startDatetime; dt.Value.CompareTo(endDatetime) <= 0; dt = dt.Value.AddDays(1)) 
       { 
        string key = dt.Value.ToString("yyyy-MM-dd"); 

        if (!dMyobject.Keys.Contains(key)) 
        { 
         dMyobject[key] = 0; 
        } 
       } 
      } 


        txtContent.Text = textfileContent; 
        lblLineCount.Text = i.ToString(); 
        //Label1.Text = this.TextBox1.Text.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Length.ToString(); 
        lblFileDate.Text = filenameDate; 




        foreach (var item in dMyobject) 
        { 
         textfileOutput += (item.Key + " " + item.Value) + Environment.NewLine; 
         // textfileOutput += (item.Value) + Environment.NewLine; 
        } 
        txtOutput.Text = textfileOutput; 



       } 


    //this method is a new method, it got total score, and if you rule chanage you can set `D` +1, `A` +2 etc. 
      private int GetTotal(MatchCollection collection) 
      { 
       Dictionary<string, int> point = new Dictionary<string, int>(); 
       point["D"] = 1; 
       point["A"] = 0; 

       int total = 0; 
       foreach (Match m in collection) 
       { 
        string str = m.Groups["c"].Value; 
        if (point.ContainsKey(str)) 
         total += point[str]; 
       } 
       return total; 
      } 
+0

這是因爲它概括了所有的總「d」和「A‘的發生,如什麼該問題時,應該算’d沒有得到期望的輸出; 「出現PER日期,請查看所需的輸出,謝謝 – rickyProgrammer

+0

我已經修好了,使用我的代碼,你可以定義每個字母的點,例如:如果每個D增加1點,每個A增加2點, O'扣1分,然後用我的代碼很容易滿足這些。 –

0

添加註釋JohanP答案,代碼工作正常預期日期子串是錯誤的原代碼,並有與字典沒有問題0值

+0

缺乏,那是當第一個日期爲空。 – rickyProgrammer

0

JohanP的答案是一個很大的幫助,但也有侷限性,因爲它也當它爲空時,需要寫出第一個日期。基於他的回答,我已經提出了這些代碼。

using (StreamReader stRead = new StreamReader(FileUpload1.PostedFile.InputStream)) 
     { 

      Dictionary<string, int> dMyobject = new Dictionary<string, int>(); 

      while (!stRead.EndOfStream) 
      { 
       var readedLine = stRead.ReadLine(); 
       if (!string.IsNullOrWhiteSpace(readedLine)) 
       { 
        //int readedLineTime = Convert.ToInt32(readedLine.Substring(11, 02)); 
        string sDate = readedLine.Substring(11, 2); 

        MatchCollection collection = Regex.Matches(readedLine, @"D;"); 
        countedChars = collection.Count; 


        if (!dMyobject.Keys.Contains(sDate)) 
        { 
         dMyobject.Add(sDate, collection.Count); 
        } 
        else 
        { 
         dMyobject[sDate] = dMyobject[sDate] + collection.Count; 
        } 
       } 
       textfileContent += readedLine + Environment.NewLine; 
       i++; 
      } 
      txtContent.Text = textfileContent; 
      lblLineCount.Text = i.ToString(); 



      var prevDate = string.Empty; 
      int tester = 01; 

      foreach (var item in dMyobject) 
      { 
       int testCorrectStart = Convert.ToInt32(item.Key) - tester; 
       if (testCorrectStart == 0) 
       { 
        if (!string.IsNullOrEmpty(prevDate)) 
        { 
         var cur = Int32.Parse(item.Key); // convert current key into int 
         var prev = Int32.Parse(prevDate); 
         int dayDiff = cur - prev; 
         for (var x = 0; x < dayDiff - 1; x++) // run through day difference, add it to the last date that was added 
         { 

          textfileOutput += ((prev + (x + 1)).ToString() + " 0" + Environment.NewLine); 

         } 
        } 

        textfileOutput += (item.Key + " " + item.Value) + Environment.NewLine; 
        prevDate = item.Key; 
        tester++; 
       } 

       else 
       { 

        if (!string.IsNullOrEmpty(tester.ToString())) 
        { 
         var cur = Int32.Parse(item.Key); // convert current key into int 
         var prev = Int32.Parse(tester.ToString()); 
         int dayDiff = cur - prev; 
         for (var x = 0; x < dayDiff ; x++) // run through day difference, add it to the last date that was added 
         { 

          textfileOutput += ("0" +(prev + x).ToString() + " 0" + Environment.NewLine); 
         } 
        } 
        textfileOutput += (item.Key + " " + item.Value) + Environment.NewLine; 
        prevDate = item.Key; 
        tester = Convert.ToInt32(prevDate) + 1; 
       } 
      } 
      txtOutput.Text = textfileOutput; 
     } 

可以測試其correcteness

相關問題