在C#中使用自定义格式解析文本文件

时间:2014-07-24 08:15:26

标签: c# .net parsing

我有一堆具有自定义格式的文本文件,如下所示:

App Name    
Export Layout

Produced at 24/07/2011 09:53:21


Field Name                             Length                                                       

NAME                                   100                                                           
FULLNAME1                              150                                                           
ADDR1                                  80                                                           
ADDR2                                  80          

任何空格都可以是制表符或空格。该文件可能包含任意数量的字段名称和长度。

我想获取所有字段名称及其相应的字段长度,并可能将它们存储在字典中。该信息将用于处理具有所述字段名称和字段长度的相应固定宽度数据文件。

我知道如何使用ReadLine()跳过行。我不知道的是怎么说:“当你到达以'Field Name'开头的行时,再跳过一行,然后从下一行开始,抓住左栏中的所有单词和上面的数字。右栏。“

我已经尝试过String.Trim(),但这并没有删除之间的空格。

提前致谢。

3 个答案:

答案 0 :(得分:5)

您可以使用SkipWhile(l => !l.TrimStart().StartsWith("Field Name")).Skip(1)

Dictionary<string, string> allFieldLengths = File.ReadLines("path")
    .SkipWhile(l => !l.TrimStart().StartsWith("Field Name")) // skips lines that don't start with "Field Name"
    .Skip(1)                                       // go to next line
    .SkipWhile(l => string.IsNullOrWhiteSpace(l))  // skip following empty line(s)
    .Select(l =>                                   
    {                                              // anonymous method to use "real code"
        var line = l.Trim();                       // remove spaces or tabs from start and end of line
        string[] token = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        return new { line, token };                // return anonymous type from 
    })
    .Where(x => x.token.Length == 2)               // ignore all lines with more than two fields (invalid data)
    .Select(x => new { FieldName = x.token[0], Length = x.token[1] })
    .GroupBy(x => x.FieldName)                     // groups lines by FieldName, every group contains it's Key + all anonymous types which belong to this group
    .ToDictionary(xg => xg.Key, xg => string.Join(",", xg.Select(x => x.Length)));

line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)将按空格和制表符分割,并忽略所有空格。使用GroupBy确保所有键在字典中都是唯一的。如果是重复的字段名称,Length将以逗号连接。


编辑:既然您已申请非LINQ版本,请按以下步骤操作:

Dictionary<string, string> allFieldLengths = new Dictionary<string, string>();
bool headerFound = false;
bool dataFound = false;
foreach (string l in File.ReadLines("path"))
{
    string line = l.Trim();
    if (!headerFound && line.StartsWith("Field Name"))
    {
        headerFound = true;
        // skip this line:
        continue;
    }
    if (!headerFound)
        continue;
    if (!dataFound && line.Length > 0)
        dataFound = true;
    if (!dataFound)
        continue;
    string[] token = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    if (token.Length != 2)
        continue;
    string fieldName = token[0];
    string length = token[1];
    string lengthInDict;
    if (allFieldLengths.TryGetValue(fieldName, out lengthInDict))
        // append this length
        allFieldLengths[fieldName] = lengthInDict + "," + length;
    else
        allFieldLengths.Add(fieldName, length);
}

我更喜欢LINQ版本,因为它更具可读性和可维护性(imo)。

答案 1 :(得分:1)

基于标题行的位置固定的假设,我们可以考虑从第9行开始的实际键值对。然后,使用ReadAllLines方法从文件中返回String数组,我们只需从索引8开始处理:

  string[] lines = File.ReadAllLines(filepath);
  Dictionary<string,int> pairs = new Dictionary<string,int>();

    for(int i=8;i<lines.Length;i++)
    {
        string[] pair = Regex.Replace(lines[i],"(\\s)+",";").Split(';');
        pairs.Add(pair[0],int.Parse(pair[1]));
    }

这是一个骨架,不考虑异常处理,但我想它应该让你开始。

答案 2 :(得分:0)

您可以使用String.StartsWith()来检测“FieldName”。然后String.Split()参数为null,用空格分割。这将为您提供您的字段名和长度字符串。