如何删除CSV文件中的空白行? [英] How to remove blank lines in a CSV file?

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

问题描述

你好,



我想知道如何在用C#编写的应用程序中自动删除CSV文件中的空白行。我的应用程序目前能够读取电子表格并将其转换为CSV文件。



如有任何信息,将不胜感激。



问候,



Glen。

Hello,

I would like to know how to be able to remove blank rows in a CSV file automatically in an app written in C#. My application currently is able to read a spreadsheet and convert this to the CSV file.

Any info will be gratefully appreciated.

Regards,

Glen.

推荐答案

听起来更复杂:空白行可能是这样的:

That's more complex that it sounds: a "blank line" could be this:
1,2,3

4,5,6

删除内容非常简单:

Which is trivial to remove:

File.WriteAllLines(fileName,File.ReadAllLines(fileName).Where(l => !string.IsNullOrWhiteSpace(l)));

或者它可以引用这个:

Or it could refer to this:

1,2,3
,,,
4,5,6

哪个更难 - 它需要正确解析,因为它可能是

Which is a lot harder - it requires "proper" parsing, because it could be

"1","2","3"
"","",""
"4","5","6"

在这种情况下,你真的想看看你正在阅读它的结构,并在写入CSV文件之前直接删除它的空实例。



我们无法帮助您:我们不知道您的软件是如何工作的!

In this case, you really want to look at the structures that you are reading this into, and remove empty instances of that directly before writing the CSV file.

And we can't help you with that: we have no idea how your software works!


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

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