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);




                    }
                }







            }


        }
    }
4

1 回答 1

1

您可以使用 System.IO.File 类的各种方法创建文件:

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

此类包括用于创建文件以及将任意文本行写入其中的方法。

您可以使用 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

这只是非常粗略和准备,但应该指出编写此代码所需的思考方式。需要明确的一点是,如果您想使用日期块的结束日期命名文件,则在找到该块中的最后一行之前无法创建文件,因此您需要将这些行存储在内存中直到您发现日期中的空白或输入文件的结尾。

于 2013-07-27T07:46:58.077 回答