取一个文件并将其分成两组

时间:2015-06-04 16:54:35

标签: c# list file-io

所以下面我有一段代码,它将采用一个数据文件并分成两组,a和b。

string path = @"c:\users\povermyer\documents\visual studio 2013\Projects\DanProject\PNRS\PNRS.log";

string[] lines = System.IO.File.ReadAllLines(path);
var count = File.ReadLines(path).Count();
List<string> groupA = lines.Take(7678).ToList();
List<string> groupB = lines.Skip(7678).Take(5292).ToList();

为了澄清,第一组采用代码的前7678行并将其放入组中,而第二组跳过前7678行并将其余行(5292行)放入组中。唯一的问题是,如果我要使用未来的文件,它可能不包含7678的拳头和5292.我知道第一组的开头以A开头,以A结尾,第二组以B以B结尾。所以我的问题是,如何将上面的代码放到两组中,具体取决于它们的开始和结束方式?

此外,开始和结束的行并不是唯一的。例如,a的开头是

***********BEGIN PROCESSING A PNRS*********** and the end is ************END PROCESSING A PNRS************` 

和B组一样。请帮忙!

2 个答案:

答案 0 :(得分:1)

这个怎么样:

List<string> groupA = lines.Where(s => s.StartsWith("A") && s.EndsWith("A")).ToList();
List<string> groupB = lines.Where(s => s.StartsWith("B") && s.EndsWith("B")).ToList();

哦,而且,我知道这不是你的问题,而是......而不是

var count = File.ReadLines(path).Count();

......为什么不简单地这样做:

var count = lines.Length;

它避免了必须两次读取文件。

答案 1 :(得分:1)

如果您需要分组更多的组,您可能需要考虑将组存储在Dictionary<string, List<string>>中,其中键是组名,值是仅包含组数据的列。 / p>

更新

如果我了解该方案,请说明您的数据如下:

"***********BEGIN PROCESSING A PNRS*********** the beginning is 1 ************END PROCESSING A PNRS************",
"***********BEGIN PROCESSING A PNRS*********** the beginning is 2 ************END PROCESSING A PNRS************",
"***********BEGIN PROCESSING B PNRS*********** and the end is 1 ************END PROCESSING B PNRS************",
"***********BEGIN PROCESSING B PNRS*********** and the end is 2 ************END PROCESSING B PNRS************",
"***********BEGIN PROCESSING AB PNRS*********** good morning to you 1 ************END PROCESSING AB PNRS************",
"***********BEGIN PROCESSING AB PNRS*********** good morning to you 2 ************END PROCESSING AB PNRS************"

您希望将其分组为:

A: 
[0] the beginning is 1
[1] the beginning is 2
B:
[0] and the end is 1
[1] and the end is 2
AB:
[0] good morning to you 1
[1] good morning to you 2

这可能最适合Regular Expressions,我仍然建议将所有内容存储在Dictionary<string, List<string>>

新代码

/// <summary>
/// Separates the List of string data into groups of data
/// </summary>
/// <param name="data">Array of string data</param>
/// <param name="groupNames">Array of group names</param>
/// <returns>Dictionary of List of string data broken into groups</returns>
private static Dictionary<string, List<string>> SeparateGroups(string[] data, params string[] groupNames)
{
    return groupNames.ToDictionary(
        groupName => groupName,
        groupName => data.Select(d => {
            Match m = Regex.Match(d, String.Format("^\\*{{11,}}BEGIN PROCESSING {0} PNRS\\*{{11,}}\\s(.*)\\s\\*{{11,}}END PROCESSING {0} PNRS\\*{{11,}}$", groupName));
            return m.Success ? m.Groups[1].Value : String.Empty;
        }).Where(s => !String.IsNullOrEmpty(s)).ToList()
    );
}

用法:

string[] groupNames = new[] { "A", "B" , "AB" };
string[] lines = new[] {
    "***********BEGIN PROCESSING A PNRS*********** the beginning is 1 ************END PROCESSING A PNRS************",
    "***********BEGIN PROCESSING A PNRS*********** the beginning is 2 ************END PROCESSING A PNRS************",
    "***********BEGIN PROCESSING B PNRS*********** and the end is 1 ************END PROCESSING B PNRS************",
    "***********BEGIN PROCESSING B PNRS*********** and the end is 2 ************END PROCESSING B PNRS************",
    "***********BEGIN PROCESSING AB PNRS*********** good morning to you 1 ************END PROCESSING AB PNRS************",
    "***********BEGIN PROCESSING AB PNRS*********** good morning to you 2 ************END PROCESSING AB PNRS************"
};

int count = lines.Length;
Dictionary<string, List<string>> groups = SeparateGroups(lines, groupNames);

foreach (string key in groups.Keys)
{
    Console.WriteLine(key + ":");
    foreach (string value in groups[key])
    {
        Console.WriteLine(value);
    }
}

结果:

A:
the beginning is 1
the beginning is 2
B:
and the end is 1
and the end is 2
AB:
good morning to you 1
good morning to you 2

旧代码

/// <summary>
/// Separates the List of string data into groups of data
/// </summary>
/// <param name="data">Array of string data</param>
/// <param name="groupNames">Array of group names</param>
/// <returns>Dictionary of List of string data broken into groups</returns>
private Dictionary<string, List<string>> SeparateGroups(string[] data, params string[] groupNames)
{
    return groupNames.ToDictionary(
        groupName => groupName, 
        groupName => data.Where(ag => ag.StartsWith(groupName) && ag.EndsWith(groupName)).ToList()
    );
}

用法:

string[] groupNames = new[] { "A", "B", "AB" };
string[] lines = File.ReadAllLines(filePath);
int count = lines.Length
Dictionary<string, List<string>> groups = SeparateGroups(lines, groupNames);