从 CSV 中删除行 [英] Delete rows from CSV

查看:50
本文介绍了从 CSV 中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 SSIS 中使用 C# 从 CSV 文件中删除行

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

这是我的文件

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

基本上我需要删除

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

提前致谢

推荐答案

这是一个基于 5 个逗号标准的简单解决方案

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

写出来:

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

这篇关于从 CSV 中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆