C# 如何跳过txt文件中的行
嘿,伙计们,我一直有一些麻烦跳过一些不必要的行从txt文件,我读到我的程序。数据的格式如下:C# 如何跳过txt文件中的行,c#,C#,嘿,伙计们,我一直有一些麻烦跳过一些不必要的行从txt文件,我读到我的程序。数据的格式如下: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 我想读第1行,修剪第3行、第4行和空白,然后读第5行,修剪第7行和第8行。我在这个网站上读到了类似的内容,但是,那个特殊的案例跳过了文本文件的前5行。这就是我迄今为止所尝试的: string TextLine; System.IO.StreamRead
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
我想读第1行,修剪第3行、第4行和空白,然后读第5行,修剪第7行和第8行。我在这个网站上读到了类似的内容,但是,那个特殊的案例跳过了文本文件的前5行。这就是我迄今为止所尝试的:
string TextLine;
System.IO.StreamReader file =
new System.IO.StreamReader("C://log.txt");
while ((TextLine = file.ReadLine()) != null)
{
foreach (var i in Enumerable.Range(2, 3)) file.ReadLine();
Console.WriteLine(TextLine);
}
正如你们所看到的,对于范围,我已经将开始指定为第2行,然后跳过3行,其中包括空白。然而,Enumerable.Range的第一个参数似乎并不重要。我可以把一个0,它将产生相同的结果。正如我现在看到的,程序从第一行开始修剪,直到.Range函数的第二个参数中指定的数字。有人知道解决这个问题的方法吗?谢谢当然范围不重要。。。您所做的是在每次while循环迭代中一次跳过2行-2-3对文件读取器指针没有影响。我建议你只需要有一个计数器告诉你你在哪一行,如果行号是你想跳过的行号,就跳过
int currentLine = 1;
while ((TextLine = file.ReadLine()) != null)
{
if ( LineEnabled( currentLine )){
Console.WriteLine(TextLine);
}
currentLine++;
}
private boolean LineEnabled( int lineNumber )
{
if ( lineNumber == 2 || lineNumber == 3 || lineNumber == 4 ){ return false; }
return true;
}
可枚举的文档。范围状态:
因此,更改第一个参数不会更改程序的逻辑
然而,这是一种奇怪的方法。for循环将更简单、更容易理解和更高效
另外,您的代码当前读取第一行,跳过三行,然后输出第一行,然后重复。您尝试过类似的方法吗
using (var file = new StreamReader("C://log.txt"))
{
var lineCt = 0;
while (var line = file.ReadLine())
{
lineCt++;
//logic for lines to keep
if (lineCt == 1 || lineCt == 5)
{
Console.WriteLine(line);
}
}
}
尽管除非这是一个非常固定格式的输入文件,否则我会找到一种不同的方法来决定如何处理每一行,而不是一个固定的行号。为什么不将所有行读取到一个数组中,然后对所需的行进行索引呢
var lines = File.ReadAllLines("C://log.txt");
Console.WriteLine(lines[0]);
Console.WriteLine(lines[5]);
如果它是一个非常大的文件,具有一致的重复部分,则可以创建一个读取方法并执行以下操作:
while (!file.EndOfStream)
{
yield return file.ReadLine();
yield return file.ReadLine();
file.ReadLine();
file.ReadLine();
file.ReadLine();
}
或类似的块格式。我认为您不想在两个位置读取行,一个在循环中,另一个在循环中。我会采取这个方法:
while ((TextLine = file.ReadLine()) != null)
{
if (string.IsNullOrWhitespace(TextLine)) // Or any other conditions
continue;
Console.WriteLine(TextLine);
}
这是OP要求提供的解决方案的扩展版本
public static IEnumerable<string> getMeaningfulLines(string filename)
{
System.IO.StreamReader file =
new System.IO.StreamReader(filename);
while (!file.EndOfStream)
{
//keep two lines that we care about
yield return file.ReadLine();
yield return file.ReadLine();
//discard three lines that we don't need
file.ReadLine();
file.ReadLine();
file.ReadLine();
}
}
public static void Main()
{
foreach(string line in getMeaningfulLines(@"C:/log.txt"))
{
//or do whatever else you want with the "meaningful" lines.
Console.WriteLine(line);
}
}
这里是另一个版本,如果输入文件突然结束,它会稍微不那么脆弱
//Just get all lines from a file as an IEnumerable; handy helper method in general.
public static IEnumerable<string> GetAllLines(string filename)
{
System.IO.StreamReader file =
new System.IO.StreamReader(filename);
while (!file.EndOfStream)
{
yield return file.ReadLine();
}
}
public static IEnumerable<string> getMeaningfulLines2(string filename)
{
int counter = 0;
//This will yield when counter is 0 or 1, and not when it's 2, 3, or 4.
//The result is yield two, skip 3, repeat.
foreach(string line in GetAllLines(filename))
{
if(counter < 2)
yield return line;
//add one to the counter and have it wrap,
//so it is always between 0 and 4 (inclusive).
counter = (counter + 1) % 5;
}
}
使用StringBuilder操纵文本。您的最终意图是什么?要阅读每组的前两行以空行分隔?我建议保留一组行,而不是一堆OR,除非最终产品中只有3行。还假设没有可利用的模式。是的,当然,还有更多,这将是首选way@Servy,但一旦txt文件变大,效率就会降低。感谢您的回复。这对中小型文件非常有效。不是大文件。假设这是一个实际问题,只是一个简单的例子,它会起作用。如果这将被外推到一个大文件,它将浪费大量内存。是的,但示例没有让它听起来很大:@Servy这确实有效,是的,现在文件很小。但是,不断有数据附加到文件中,因此文件最终会变大。谢谢您的输入。我喜欢“编辑中”选项,假设它遵循的模式是简单、有效、可伸缩且可读的。@Servy,它实际上是一致的重复部分。因此,基本上,第1-8行将继续以该格式重复,当然,使用不同的数据。你能解释一下编辑的选项吗。streamreader不是我的reader方法吗?收益率是否会返回这些特定的行?非常感谢您的帮助和快速响应,这正是我所寻找的!
//Just get all lines from a file as an IEnumerable; handy helper method in general.
public static IEnumerable<string> GetAllLines(string filename)
{
System.IO.StreamReader file =
new System.IO.StreamReader(filename);
while (!file.EndOfStream)
{
yield return file.ReadLine();
}
}
public static IEnumerable<string> getMeaningfulLines2(string filename)
{
int counter = 0;
//This will yield when counter is 0 or 1, and not when it's 2, 3, or 4.
//The result is yield two, skip 3, repeat.
foreach(string line in GetAllLines(filename))
{
if(counter < 2)
yield return line;
//add one to the counter and have it wrap,
//so it is always between 0 and 4 (inclusive).
counter = (counter + 1) % 5;
}
}