繁体   English   中英

从文本文件C#中提取特定的文本行

[英]Extract specific lines of text from a text file c#

我正在使用StreamReader逐行读取文本文件,并将其显示到控制台。 它读入并显示正常。 每行从左到右包含日期,时间,数字,条目。 文本文件包含数百行文本。 我想允许用户输入特定的日期和时间,并且只获取该时间范围内的文本行。 因此,在数百行文本中,只能针对输入的日期和时间返回文本行。

到目前为止,这是我的代码,可以阅读并显示文本文件。

    public override void ReadFile(string strFileName)
    {
        try
        {
            using (StreamReader sr = new StreamReader(@"C:\MyFolder\TextFile.txt"))
            {
                String line = sr.ReadLine();
                Console.WriteLine(line);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }

这可以正常工作并在控制台上显示文本,如下所示:

01-01-2015 10:10:10 Line of text blah blah blah
01-01-2015 10:10:10 Line of text blah blah blah
01-01-2015 10:10:10 Line of text blah blah blah

现在,我试图将行分开以获取各个部分,例如日期和时间。 到目前为止,这是我所拥有的,但是我不确定什么是最好的进行方式。

        public override void ReadLiFile(string strFileName)
    {
        try
        {
            using (StreamReader sr = new StreamReader(@"C:\MyFolder\TextFile.txt"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] fields = line.Split("\t".ToCharArray()); 
                    int theInt = Convert.ToInt32(fields[0]);//to get first field e.g Date
                }
                Console.WriteLine(line);
            }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }

我想根据给定的日期和时间缩小输出到屏幕的范围。 我不希望所有行仅显示与特定日期和时间相关的行,例如在01-01-2015 10:10:10和01-01-2015 10:15:15之间的行

我猜我需要隔离每一行中的日期和时间字段并存储值。 我试图拆分线并存储第一个值是Date。 我的问题是拆分行中字段的最佳方法是什么? 我的尝试不起作用。 我现在正得到一条错误消息,提示“无法读取文件”以及“输入字符串的格式不正确”。谢谢您的帮助。

这将提取日期部分并将其解析为DateTime ,然后您可以对其进行过滤。

var startDate = new DateTime(2015, 1, 1, 10, 10, 10);
var endDate = new DateTime(2015, 1, 1, 10, 15, 15);

var result = (from line in File.ReadLines(@"C:\MyFolder\TextFile.txt")
              let pieces = line.Split('\t')
              let date = DateTime.ParseExact(pieces[0] + pieces[1], "MM-dd-yyyyHH:mm:ss", CultureInfo.InvariantCulture)
              where date >= startDate && date <= endDate
              select line).ToList();

这是LINQ,以防您以前从未看过它。 一旦习惯了,它对查询非常有用。

let关键字允许您存储数据,这样(例如)您可以在整个查询中多次使用它,而无需多次执行ParseExactSplit

File.ReadLines从文件中读取尽可能少的行,足以满足您的查询。 尽管在这种情况下,必须解析每一行以检查日期,但无论如何,它始终在读取它们。

DateTime dt;
if (DateTime.TryParse(fields[0] + " " + fields[1], out dt))
    if (dt >= minDate && dt <= maxDate)

这应该在startDateendDate之间打印所有带有Date的行。 我已经省略了时间部分,但是您应该了解如何做。

DateTime startDate = new DateTime(2015, 1, 1);
DateTime endDate = new DateTime(2015, 1, 3);
var query = File.ReadLines(@"e:\1.txt").Select(line => new { Line = line, Date = Convert.ToDateTime(line.Split(' ').First()) })
                                        .Where(x => x.Date >= startDate && x.Date <= endDate);

暂无
暂无

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

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