【发布时间】:2017-12-18 12:46:53
【问题描述】:
我有一个文本文件,它被分成许多部分,每个部分大约 10 行左右。我正在使用 File.ReadAllLines 将文件读入一个数组,数组的每个元素一行,然后我试图解析文件的每个部分以仅带回一些数据。我将结果存储在一个列表中,并希望最终将列表导出到 csv。
我的 for 循环给我带来了麻烦,因为它循环了正确的次数,但每次只从文本文件的第一部分提取数据,而不是从第一部分提取数据然后继续从下一节中提取数据。我确定我在 for 循环或每个循环中都做错了什么。任何帮助我解决这个问题的线索将不胜感激!谢谢 大卫
到目前为止我的代码:
namespace ParseAndExport
{
class Program
{
static readonly string sourcefile = @"Path";
static void Main(string[] args)
{
string[] readInLines = File.ReadAllLines(sourcefile);
int counter = 0;
int holderCPStart = counter + 3;//Changed Paths will be an different number of lines each time, but will always start 3 lines after the startDiv
/*Need to find the start of the section and the end of the section and parse the bit in between.
* Also need to identify the blank line that occurs in each section as it is essentially a divider too.*/
int startDiv = Array.FindIndex(readInLines, counter, hyphens72);
int blankLine = Array.FindIndex(readInLines, startDiv, emptyElement);
int endDiv = Array.FindIndex(readInLines, counter + 1, hyphens72);
List<string> results = new List<string>();
//Test to see if FindIndexes work. Results should be 0, 7, 9 for 1st section of sourcefile
/*Console.WriteLine(startDiv);
Console.WriteLine(blankLine);
Console.WriteLine(endDiv);*/
//Check how long the file is so that for testing we know how long the while loop should run for
//Console.WriteLine(readInLines.Length);
//sourcefile has 5255 lines (elements) in the array
for (int i = 0; i <= readInLines.Length; i++)
{
if (i == startDiv)
{
results = (readInLines[i + 1].Split('|').Select(p => p.Trim()).ToList());
string holderCP = string.Join(Environment.NewLine, readInLines, holderCPStart, (blankLine - holderCPStart - 1)).Trim();
results.Add(holderCP);
string comment = string.Join(" ", readInLines, blankLine + 1, (endDiv - (blankLine + 1)));//in case the comment is more than one line long
results.Add(comment);
i = i + 1;
}
else
{
i = i + 1;
}
foreach (string result in results)
{
Console.WriteLine(result);
}
//csvcontent.AppendLine("Revision Number, Author, Date, Time, Count of Lines, Changed Paths, Comments");
/* foreach (string result in results)
{
for (int x = 0; x <= results.Count(); x++)
{
StringBuilder csvcontent = new StringBuilder();
csvcontent.AppendLine(results[x] + "," + results[x + 1] + "," + results[x + 2] + "," + results[x + 3] + "," + results[x + 4] + "," + results[x + 5]);
x = x + 6;
string csvpath = @"addressforcsvfile";
File.AppendAllText(csvpath, csvcontent.ToString());
}
}*/
}
Console.ReadKey();
}
private static bool hyphens72(String h)
{
if (h == "------------------------------------------------------------------------")
{
return true;
}
else
{
return false;
}
}
private static bool emptyElement(String ee)
{
if (ee == "")
{
return true;
}
else
{
return false;
}
}
}
}
【问题讨论】:
-
每个部分是否以所有这些连字符开头和/或结尾? IMO 简化此操作的第一步是将 读取 和 处理 分为两个不同的任务。也许创建一个字符串列表。每次遇到连字符时,开始一个新列表并用所有文本行填充它,直到到达下一个“------------------”。然后重复。完成后,您将获得一个列表列表,其中每个 lnner 列表是连字符之间的一大块行。现在拆分完成,您可以处理它们。但不要试图将它们拆分并以一种方法处理。
标签: c# for-loop foreach export-to-csv