[英]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.