【问题标题】:C# How to parse text file by grouping?C#如何通过分组来解析文本文件?
【发布时间】:2010-12-10 06:19:25
【问题描述】:

我有一个简单的程序,它读取一个日志文本文件并尝试解析它。

该程序可以通过“---------------------”“分组”/解析日志文本文件,我尝试使用“.split”方法,但它不起作用。

基本上,如果可能的话,我希望程序将文本文件从每个“----------------”到“----------- ----" 用于其他进程。

有人可以就代码提供建议吗?谢谢!

我的代码:

class Program
{
    static void Main(string[] args)
    {

        System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("C:\\Syscrawl\\new.txt");

        foreach (String r in lines.Skip(7))
        {

            String[] token = r.Split('-');

            foreach (String t in token)
            {
                Console.WriteLine(t);
            }
        }
    }
}

文本文件的示例;

Restore Point Info
Description   : Installed Apache HTTP Server 2.2.16
Type          : Application Install
Creation Time : Thu Dec  9 08:04:46 2010

C:\syscrawl\Restore\RP10\snapshot\_REGISTRY_USER_NTUSER_S-
1-5-21-1390067357-413027322-1801674531-500

Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs not found.
----------------------------------------
Restore Point Info
Description   : Testing 0
Type          : System CheckPoint
Creation Time : Thu Dec  9 08:05:43 2010

C:\syscrawl\Restore\RP11\snapshot\_REGISTRY_USER_NTUSER_S-
1-5-21-1390067357-413027322-1801674531-500

Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs not found.
----------------------------------------
Restore Point Info
Description   : Installed Python 2.4.1
Type          : Application Install
Creation Time : Thu Dec  9 08:09:12 2010

C:\syscrawl\Restore\RP12\snapshot\_REGISTRY_USER_NTUSER_S-
1-5-21-1390067357-413027322-1801674531-500

Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs not found.
----------------------------------------
Restore Point Info
Description   : Installed AccessData FTK Imager.
Type          : Application Install
Creation Time : Thu Dec  9 08:14:02 2010

C:\syscrawl\Restore\RP13\snapshot\_REGISTRY_USER_NTUSER_S-
1-5-21-1390067357-413027322-1801674531-500

Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs not found.

【问题讨论】:

    标签: c# parsing text


    【解决方案1】:

    一个相当简单的迭代器可以为您提供分隔线之间的一系列线条:

    static IEnumerable<IList<string>> ParseLines(IEnumerable<string> lines)
    {
        var lineSet = new List<string>();
        foreach(var line in lines)
        {
            if(line.StartsWith("----"))
            {
                yield return lineSet;
                lineSet = new List<string>();
            }
            else
            {
                lineSet.Add(line);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您的问题是您要拆分每一行,而不是将文件拆分为单个数据。

      string fileContent = File.ReadAllText("C:\\Syscrawl\\new.txt");
      var logItems = fileContent.Split(new string[]{"----------------------"}, false);
      

      当您在屏幕上渲染时,每个logItems 仍将包含其换行符。我会将它们保留为单个数据,而不是将它们分成几行。

      【讨论】:

      • 产生了很多错误。 “Split”方法不能将String作为变量进行拆分,而只能是数组。
      猜你喜欢
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      相关资源
      最近更新 更多