我有一堆具有自定义格式的文本文件,如下所示:
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() 但这并没有删除 之间的空格。
提前致谢。
最佳答案
您可以使用 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)。
关于c# - 在 C# 中解析具有自定义格式的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24928425/