C#如何使用CSVReader捕获CSV文件中的空行?

编程入门 行业动态 更新时间:2024-10-28 09:18:40
本文介绍了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文件中的空行?

本文发布于:2023-05-25 10:59:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空行   如何使用   文件   CSV   CSVReader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!