2013-07-27 78 views
1

我忙於處理結構如下所示的文本文件:
這是連續字符串中的降雨量數據,日期後每5個字符表示一個月中的某一天。將特定行保存到新的文本文件

0005880 W 1926 9-7777-7777-7777-7777-7777-7777-7777-7777-7777 117 130 64-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 
0005880 W 192610-7777-7777-7777-7777-7777-7777-7777-7777-7777 23-7777-7777-7777-7777 3-7777 226 462 71-7777-7777 157 76 15-7777-7777-7777-7777-7777-7777-7777 
0005880 W 192611 3 20-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 61 142-7777-7777-7777 8-7777-7777-7777-7777 
0005880 W 192612-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 132-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777-7777 

年和月表示在字符串中的(10,4)和(14,2)位置。 我的問題是,有一些情況下,下一行不是要遵循的月份。我寫了一些代碼,在下面顯示的一個月的數據丟失的地方添加了一行。

public void findGapsToolStripMenuItem_Click(object sender, EventArgs e) 
    { 


     TabPage tp = new TabPage(); 
     RichTextBox rtb = new RichTextBox(); 
     rtb.Dock = DockStyle.Fill; 
     rtb.Multiline = true; 
     rtb.AcceptsTab = true; 
     rtb.WordWrap = false; 





     Stream myStream; 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

     if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      if ((myStream = openFileDialog1.OpenFile()) != null) 
      { 


       tp.Controls.Add(rtb); 
       tabControl1.TabPages.Add(tp); 


       string strfilename = openFileDialog1.FileName; 
       string[] lines = File.ReadAllLines(strfilename); 
       string[] pathArr = strfilename.Split('\\'); 
       string[] fileArr = pathArr.Last().Split(); 
       string filen = fileArr.Last().ToString(); 



       tp.Text = filen; 

       int pyear = 0; 
       int pmon = 0; 
       int imon = 0; 
       int iyear = 0; 


       foreach (string line in lines) 
       { 
        string missing = "-9999"; 
        string year = line.Substring(10, 4); 
        string mon = line.Substring(14, 2); 
        iyear = Convert.ToInt32(year); 
        imon = Convert.ToInt32(mon); 


        if (pyear == 0) 
        { 
         pyear = iyear; 
         pmon = imon; 

         rtb.AppendText(line + "\n"); 

        } 
        else 
        { 
         int pt = pyear * 12 + pmon; 
         int t = iyear * 12 + imon; 
         if ((pt + 1) == t) 
         { 

          rtb.AppendText(line + "\n"); 
         } 
         else 
         { 

          rtb.AppendText("Missing Months =" + (t - pt) + "\n"); 

         } 

         if (line.Contains(missing)) 
         { 
          rtb.AppendText("Missing Days" + "\n"); 


         } 


         pyear = iyear; 
         pmon = imon; 
        } 

        rtb.SelectAll(); 
        rtb.SelectionAlignment = HorizontalAlignment.Left; 
        rtb.SelectionFont = new Font("Consolas", 10); 








       } 

      } 

     } 


    } 

我的問題是,是否有失蹤一個月或一天命名失蹤一個月或前一天的開始日期的日期文本文件之前導出所有行的方式。例如,1926.9.1926.10.txt。然後在下一個缺失的月份或一天之前繼續完成下一部分數據的文件。所以基本上就是以多個文本文檔結尾,這些文檔包含幾年或幾個月的數據,但沒有空白我也想它自動創建,所有的文本文件將被創建的站號是第14個字符(即0005880 W)的文件夾。

更新

public void findGapsToolStripMenuItem_Click(object sender, EventArgs e) 
    { 


     TabPage tp = new TabPage(); 
     RichTextBox rtb = new RichTextBox(); 
     rtb.Dock = DockStyle.Fill; 
     rtb.Multiline = true; 
     rtb.AcceptsTab = true; 
     rtb.WordWrap = false; 





     Stream myStream; 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

     if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      if ((myStream = openFileDialog1.OpenFile()) != null) 
      { 


       tp.Controls.Add(rtb); 
       tabControl1.TabPages.Add(tp); 


       string strfilename = openFileDialog1.FileName; 
       string[] lines = File.ReadAllLines(strfilename); 
       string[] pathArr = strfilename.Split('\\'); 
       string[] fileArr = pathArr.Last().Split(); 
       string filen = fileArr.Last().ToString(); 
       string pat = @"C:\Test\" + filen; 
       System.IO.Directory.CreateDirectory(pat); 
       int i; 





       tp.Text = filen; 

       int pyear = 0; 
       int pmon = 0; 
       int imon = 0; 
       int iyear = 0; 
       int j = 1; 


       foreach (string line in lines) 
       { 
        using (StreamWriter sw = new StreamWriter(@"C:\Test\" + filen+".txt")) 
        { 



         string missing = "-9999"; 
         string year = line.Substring(10, 4); 
         string mon = line.Substring(14, 2); 
         iyear = Convert.ToInt32(year); 
         imon = Convert.ToInt32(mon); 
         string filepath = @"C:\Test\" + year + "." + mon+".txt"; 

         if (pyear == 0) 
         { 

          File.CreateText(filepath); 
          pyear = iyear; 
          pmon = imon; 

          rtb.AppendText(line + "\n"); 

          sw.WriteLine(line); 


         } 
         else 
         { 

          File.CreateText(filepath); 
          int pt = pyear * 12 + pmon; 
          int t = iyear * 12 + imon; 
          if ((pt + 1) == t) 
          { 

           rtb.AppendText(line + "\n"); 
           sw.WriteLine(line); 


          } 
          else 
          { 

           string path = pat + "\\" + year + "." + mon + ".txt"; 
           File.CreateText(path); 

           rtb.AppendText("Missing Months =" + (t - pt) + "\n"); 

          } 

          if (line.Contains(missing)) 
          { 

           string path = pat + "\\" + year + "." + mon + ".txt"; 
           File.CreateText(path); 
           rtb.AppendText("Missing Days" + "\n"); 


          } 


          pyear = iyear; 
          pmon = imon; 


         } 

         rtb.SelectAll(); 
         rtb.SelectionAlignment = HorizontalAlignment.Left; 
         rtb.SelectionFont = new Font("Consolas", 10); 




        } 
       } 







      } 


     } 
    } 

回答

1

http://msdn.microsoft.com/en-us/library/system.io.file.aspx

該類包含用於創建文件以及寫出ARB方法:可以用有System.IO.File類的各種方法創建文件將文字行寫成一行。

您可以使用該System.IO.Directory類的方法目錄:

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

更新:這裏是一些僞

startdate = null 
foreach(line in the input file) 
{ 
    currentdate = date on this line in the input file 

    if(startdate == null) 
    { 
     // We are at the start of a new block of dates 
     startdate = currentdate   
     add this line to a list (in memory) 
    } 
    else if(currentdate == lastdate in the list + 1 month) 
    { 
     // This date is consecutive 
     add this line to a list (in memory) 
    } 
    else 
    { 
     // We have a gap in the data 
     write out all data in the list to file named <startdate>-<lastdate in list> 
     startdate = currentdate 
     add this line to the list (which we've just emptied) 
    } 
} 
write out the last file 

這只是非常粗略的和準備,但應該指出您需要考慮編寫此代碼的方式。有一件事要明確,如果你想用一個日期塊的結束日期來命名文件,那麼在找到該塊的最後一行之前,你不能創建該文件,所以你需要將行存儲在內存中直到找到日期間的差距或輸入文件的末尾。

+0

我很抱歉,仍然很困惑,看看這些鏈接。請你給我一個代碼示例,因爲我是一名水質碩士學生,並且認爲數據管理程序可以加速原始數據處理,所以在編程方面非常非常新手,所以我仍然對編程還很陌生。 –

+1

嗨。鏈接文檔中有很多示例。具體來說,我會查看目錄文檔中的'CreateDirectory(string)'方法以及File文檔中的'CreateFile('string')'和'AppendText(string)'方法。這些方法正是你所需要的。他們創建一個目錄,創建一個文件,並向該文件添加文本。如果他們不清楚或者其他問題令人困惑,那麼你能否更具體地瞭解你卡在哪裏? –

+0

您好我已經將這些納入我的代碼,但由於某種原因,它將每個文本文件寫入一行。我希望新文本文件具有所有行,直到找到一個月丟失,然後創建一個新文件。我添加我現在的代碼。再次感謝您的幫助。 –

相關問題