繁体   English   中英

读取文本文件中的多个特定行数

[英]Read Multiple Specific Number of Lines in a Text File

我在文本文件中具有以下类型的数据:

     Accumulated Earnings Tax. 
     A tax penalty which is imposed on corporate earnings which are retained by the corporation for non-
     business related needs. 

     Acquisition Cost. 
     The expenses incurred by an insurer or reinsurance company that are directly related to putting the 
     business on the books of the company. The largest portion of this cost is usually the agent's or sales 
     representative's commission or bonus. 

     Act of God. 
     An event arising out of natural causes with no human intervention which could not have been prevented 
     by reasonable care or foresight. Examples are floods, lightning, and earthquakes. 

     Actual Cash Value. 
     An amount equivalent to the replacement cost of lost or damaged property at the time of the loss, less 
     depreciation. With regard to buildings, there is a tendency for the actual cash value to closely parallel the 
     market value of the property. See also Market Value. 

每个带有说明的新单词都有换行符! 现在,我要做的是读取文件搜索关键字并打印其描述。 关键字下方但下一个由换行符标记的单词之前的行。

我已经开发了一个代码来查找关键字并打印其描述,但是它仅打印一行描述!

我的代码:

    int count = 1;
    private void openfile_Click(object sender, EventArgs e)
    {
        if (text.Text == String.Empty)
        {
            err.SetError(text, "Needs to contain Text");
        }

        DialogResult result = open_dialog.ShowDialog();

        if (result == DialogResult.OK)
        {

            try
            {
                string file_name = open_dialog.FileName;
                String lines_of_words;


                using (var file = System.IO.File.OpenText(file_name))
                {
                     // read each line, ensuring not null (EOF)
                    while ((lines_of_words = file.ReadLine()) != null)
                    {
                        if (lines_of_words.StartsWith(text.Text))
                        {

                            Console.WriteLine("TEXT IS YES"+count);
                            goGetDesc(file_name);
                            break;
                        }
                        else
                        {
                            count += 1;

                        }
                    }

                }
            }
            catch (Exception ex)
            {
            }
        }

    }

    private void goGetDesc(String file_name)
    {
        string[] lines = File.ReadAllLines(file_name);
        desc.Text+=(lines[count])+Environment.NewLine; //here i want to print the multiple lines if keyword has multiple lines!

    }

假设我要查找关键字“累计收入税”,如果找到了该关键字,则要在此关键字下打印所有行,直到LINE BREAK。

这是我的解决方案:

using (StreamReader reader = File.OpenText("some file name"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        if (line.StartsWith(textBoxInput.Text, StringComparison.OrdinalIgnoreCase))
        {
            // At this point we've already read the keyword and it matches our input
            StringBuilder description = new StringBuilder(512);
            string descLine;
            // Here we start reading description lines after the keyword.
            // Because every keyword with description is separated by blank line
            // we continue reading the file until, the last read line is empty 
            // (separator between keywords) or its null (eof)
            while ((descLine = reader.ReadLine()) != string.Empty && descLine != null)
            {
                description.AppendLine(descLine);
            }
            textBoxDescription.Text = description.ToString();
            break;
        }
    }
}

希望这可以帮助。

这是解决此问题的另一种方法,使用正则表达式并一次(而不是逐行)读取整个文件:

//using System.Text.RegularExpressions;

//the keyword you're looking for
String keyword = "Accumulated Earnings Tax";

//opening the file
using (StreamReader stream = File.OpenText("yourfile.txt"))
{
    //grabbing the whole content of the file at once
    String content = stream.ReadToEnd();                               

    //the regular expression, also see the regex101-link
    Regex re = new Regex("(" + keyword + "\\.).+?\n(.*\\.)");
    Match match = re.Match(content);
    if (match.Success)
    {
        //if the expression matches we can access our capturing groups
        //and extract the keyword and description
        Console.WriteLine("Keyword: " + match.Groups[1]);
        Console.WriteLine("Description: " + match.Groups[2]);
    }
}

regex101的示例。
基本上,您将分为两个捕获组:一个用于关键字,一个用于说明。 关键字之后必须有个. ,可能是一个或多个空格字符( .+? )和换行符( \\n )。 之后,主体小组捕获描述,直到最后一个点( (.*\\\\.) )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM