忽略文件中的空行。 [英] Ignore empty rows in a file.

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

问题描述

嘿伙计们,

我有一个问题,我需要一些帮助。我正在编写一个导入过程,在文件变为无效之前,错误阈值为20个错误。我有几个要求证明有点棘手。

我需要忽略文件末尾的空行而不将它们视为错误行(不是问题),但文件中的任何位置都可能存在空行,这会导致错误。所以我遇到的麻烦是区分文件中的一行和文件末尾的一行。请记住,文件可能是1000行。



任何帮助都是值得赞赏的人。

解决方案

< blockquote>确切的帮助很难 - 因为你没有真正指定你如何阅读文件。但是,我假设您正在使用某种形式的StreamReader逐行阅读。

不要。

使用File.ReadAllText将整个文件读入字符串。

使用String.TrimEnd修改最后的换行符

转换为具有拆分的单独行:

  Dim  text  As   String  = File .ReadAllText(  D:\Temp \Text.txt
text = text.TrimEnd()
Dim 作为 字符串()= text.Split(ControlChars.Lf)


您正在解决从文件中删除最后一个空行的一些棘手问题...特别是如果您正在处理不想加载到内存中的大文件。编码问题可能会有问题。



请参阅:[ ^ ]。


Hey Guys,
I have a question which I need a bit of help with. I am writing an import process which has an error threshold of say 20 errors before the file becomes invalid. I have a couple of requirements which are proving a bit tricky.
I need to ignore empty lines at the end of the file and not treat them as error rows (not a problem), but there may also be empty rows inside the file at any position which should cause errors. So the trouble I am having is distinguishing between a row in the file and a row at the end of the file. Bearing in mind that the file could be 1000's of lines long.

Any help is appreciated guys.

解决方案

Exact help is difficult - since you don't really specify how you are reading the file. However, I assume you are reading it line-by-line with a StreamReader of some form.
Don't.
Read the whole file with File.ReadAllText into a string.
Trim off the newlines at the end with String.TrimEnd
Convert to individual lines with Split:

Dim text As String = File.ReadAllText("D:\Temp\Text.txt")
text = text.TrimEnd()
Dim lines As String() = text.Split(ControlChars.Lf)


You are in for some tricky issues on eliminating the last empty lines from the file ... particularly if you are dealing with large files that you do not want to load into memory. Encoding issues may be problematic.

See: [^] for Jon Skeet's guru-level approach.


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

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