[英]Flat File Formatting in Console Application c#
全部 ,
我有一个平面文件,我想在其中删除末尾或中间的空行和额外的标题。 下面是格式
预期格式:
addsad, sadasd |sdsf-sdff-sfg |sffdff, sdfadgg , fg |04/15/20 12:30|sd sf 36
24567 |dsfg gfdgg fghjN |1 |fhhh
24568 |dsfg gfdgg fghjN |1 |fhhh
24569 |dsfg gfdgg fghjN |1 |fhhh
24579 |dsfg gfdgg fghjN |1 |fhhh
24571 |dsfg gfdgg fghjN |1 |fhhh
24572 |dsfg gfdgg fghjN |1 |fhhh
24573 |dsfg gfdgg fghjN |1 |fhhh
24574 |dsfg gfdgg fghjN |1 |fhhh
24575 |dsfg gfdgg fghjN |1 |fhhh
24576 |dsfg gfdgg fghjN |1 |fhhh
24577 |dsfg gfdgg fghjN |1 |fhhh
24578 |dsfg gfdgg fghjN |1 |fhhh
24579 |dsfg gfdgg fghjN |1 |fhhh
24581 |dsfg gfdgg fghjN |1 |fhhh
24582 |dsfg gfdgg fghjN |1 |fhhh
24583 |dsfg gfdgg fghjN |1 |fhhh
addsad, sadasd |sdsf-sdff-sfg |sffdff, sdfadgg , fg |04/15/20 12:30|sd sf 36
24567 |dsfg gfdgg fghjN |1 |fhhh
24568 |dsfg gfdgg fghjN |1 |fhhh
24569 |dsfg gfdgg fghjN |1 |fhhh
24579 |dsfg gfdgg fghjN |1 |fhhh
24571 |dsfg gfdgg fghjN |1 |fhhh
24572 |dsfg gfdgg fghjN |1 |fhhh
24573 |dsfg gfdgg fghjN |1 |fhhh
24574 |dsfg gfdgg fghjN |1 |fhhh
addsad, sadasd |sdsf-sdff-sfg |sffdff, sdfadgg , fg |04/15/20 12:30|sd sf 36
24575 |dsfg gfdgg fghjN |1 |fhhh
24576 |dsfg gfdgg fghjN |1 |fhhh
24577 |dsfg gfdgg fghjN |1 |fhhh
24578 |dsfg gfdgg fghjN |1 |fhhh
24579 |dsfg gfdgg fghjN |1 |fhhh
24581 |dsfg gfdgg fghjN |1 |fhhh
24582 |dsfg gfdgg fghjN |1 |fhhh
24583 |dsfg gfdgg fghjN |1 |fhhh
错误格式 2 :
addsad, sadasd |sdsf-sdff-sfg |sffdff, sdfadgg , fg |04/15/20 12:30|sd sf 36
24567 |dsfg gfdgg fghjN |1 |fhhh
24568 |dsfg gfdgg fghjN |1 |fhhh
24569 |dsfg gfdgg fghjN |1 |fhhh
24579 |dsfg gfdgg fghjN |1 |fhhh
24571 |dsfg gfdgg fghjN |1 |fhhh
24572 |dsfg gfdgg fghjN |1 |fhhh
24573 |dsfg gfdgg fghjN |1 |fhhh
24574 |dsfg gfdgg fghjN |1 |fhhh
编写的代码:(这是单独删除所有空行)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String sourceDirectory = @"C:\Console\New folder\In.txt";
String targetDirectory = @"C:\Console\New folder\Out.txt";
string text = File.ReadAllText(sourceDirectory);
text = text.Trim(' ', '\r', '\n');
File.AppendAllText(targetDirectory, text + Environment.NewLine);
}
}
}
要求:需要格式化错误的格式文件,以便我可以获得预期的格式,需要删除除第一行之外的空行并删除重复的标题。附上图片以供参考
如果遇到空行,请尝试逐行写入并忽略该行和下一行(标题)。
static void Main(string[] args)
{
var ignoreNext = false;
using var stream = new StreamWriter("targetFile.txt", true);
foreach (var line in File.ReadLines("sourceFile.txt"))
{
if (line == null || line.Trim() == string.Empty)
{
ignoreNext = true;
}
else if (!ignoreNext)
{
stream.WriteLine(line);
}
else
{
ignoreNext = false;
}
}
}
对于你的情况,当你知道你的数据是什么样子的时候,我会用稍微不同的方法来处理它,只是做......
var all = File.ReadAllLines(sourceDirectory).ToList();
var dataLines = all.Where(l => Regex.IsMatch(l, @"^\d+"));
var lines = all.Take(2).Concat(dataLines).ToList();
File.WriteAllLines(targetDirectory, lines);
不确定你是否想要第一个空行,如果你不想要,那就全部完成all.Skip(1).Take(1)
请尝试以下操作:
private void FixFile (string filename)
{
//read existing file
string[] lines = File.ReadAllLines(filename);
//overwrite existing file
using (StreamWriter sw = new StreamWriter(filename, false))
{
for (int i = 0; i < lines.Length; i++)
{
//trim leading/trailing spaces
string currentLine = lines[i].Trim();
//skip additional headers - header lines contain a date which has '/'
if (i >= 2 && !String.IsNullOrEmpty(currentLine) && !currentLine.Contains('/'))
{
sw.WriteLine(currentLine);
}
else if (i < 2)
{
//keep first two lines
sw.WriteLine(currentLine);
}
}
//clean up
sw.Flush();
sw.Close();
sw.Dispose();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.