C#如何使用CSVReader捕获CSV文件中的空行? [英] C# How to catch empty rows in a CSV file using CSVReader?

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

问题描述

我使用的是C#2005和CSVreader类,除了CSV文件末尾有一个空行外,它们都有效:

I'm using C# 2005 and the CSVreader class, all works well apart from when the CSV file contains an empty row at the end:

p>

Example:

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

我的程序错误,因为这里没有值,问题是不是所有的列都包含

My program errors as there are no values here, the problem is that not all columns are contain a value so the 2 rows below would be valid.

1,2,,4,5
2,,5,6,7

但是包含,,,,的空行不会。最后一个障碍是我不知道有多少列。

but an empty row such containing ,,,, would not. A final hurdle is I don't know for sure how many columns there will be.

你建议我如何测试一个空行?

How would you suggest I test for an empty row?

推荐答案

这个特定的CSVReader看起来对错过的字段有一些很好的处理:

This particular CSVReader looks to have some good handling of missing fields:

快速CSV阅读器

在调用阅读器之前,可以使用一些类似的代码处理一个空行:

You can handle an empty row by using some code like this before invoking the reader:

csv.MissingFieldAction = MissingFieldAction.ReplaceByNull;

或替换为空字符串:

csv.MissingFieldAction = MissingFieldAction.ReplaceByEmpty;

然后,您可以在读取空行之后循环遍历空行,如果任何列非空处理该行。

Then you could test for an empty row by looping over it after reading it, and if any column is non-null then process the row.

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

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