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

Inv*_*ion 1 .net c# parsing

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

App Name    
Export Layout

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


Field Name                             Length                                                       

NAME                                   100                                                           
FULLNAME1                              150                                                           
ADDR1                                  80                                                           
ADDR2                                  80          
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

提前致谢.

Tim*_*ter 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)));
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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