Delete rows from CSV

2019-09-21 18:07发布

问题:

I need to remove rows from CSV file using C# in SSIS

Here is my file

XXXX,,,,,,,
XXXX111,,,,,,,
XXXX222,,,,,,,
A,b,c,d,e,f
g,h,i,j,k,l
1,2,3,4,5,6
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,

Here is how my output should look like

A,b,c,d,e,f
g,h,i,j,k,l
1,2,3,4,5,6

Basically i need to remove

XXXX,,,,,,,
XXXX111,,,,,,,
XXXX222,,,,,,, 
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,

Thanks in advance

回答1:

Here's a simple solution based on your 5 commas criteria

List<String> lines = new List<string>();
string line;
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");

while ((line = file.ReadLine()) != null)
{
    lines.Add(line);
}

lines.RemoveAll(l => l.Contains(",,,,,"));

Then you can write it back out or whatever you'd like

Writing out :

using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(outputPath))
{
      outfile.Write(String.Join(System.Environment.NewLine, lines.ToArray()));
}   


标签: c# ssis