有什么办法可以忽略文本文件中某些行的读取? [英] is there any way to ignore reading in certain lines in a text file?

查看:66
本文介绍了有什么办法可以忽略文本文件中某些行的读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在c#应用程序中读取文本文件,但是我不想读取前两行或最后一行.文件中有8行,因此我只想读3、4、5、6和7行. 有什么办法吗?

I'm trying to read in a text file in a c# application, but I don't want to read the first two lines, or the last line. There's 8 lines in the file, so effectivly I just want to read in lines, 3, 4, 5, 6 and 7. Is there any way to do this?

示例文件

USE [雪莱的其他数据库]
创建表db.exmpcustomers(
fName varchar(100)NULL,
lName varchar(100)NULL,
dateOfBirth日期NULL,
houseNumber int NULL,
streetName varchar(100)NULL
)(在[PRIMARY]

USE [Shelley's Other Database]
CREATE TABLE db.exmpcustomers(
fName varchar(100) NULL,
lName varchar(100) NULL,
dateOfBirth date NULL,
houseNumber int NULL,
streetName varchar(100) NULL
) ON [PRIMARY]

编辑

好吧,所以,我已经在代码中实现了Callum Rogers的答案,由于某种原因,它可以与我编辑的文本文件一起使用(我创建了一个文本文件,但我不想使用的行省略了),并且它确实可以正常工作它应该是什么,但是每当我尝试使用原始文本文件(上面)时,它都会引发异常.我在DataGrid中显示此信息,并且我认为这是引发异常的地方.

Okay, so, I've implemented Callum Rogers answer into my code and for some reason it works with my edited text file (I created a text file with the lines I didn't want to use omitted) and it does exactly what it should, but whenever I try it with the original text file (above) it throws an exception. I display this information in a DataGrid and I think that's where the exception is being thrown.

有什么想法吗?

推荐答案

为什么要完全忽略前两行和最后一行?

Why do you want to ignore exactly the first two and the last line?

根据文件的外观,您可能需要分析该行,例如看看第一个字符是注释符号,还是忽略所有内容,直到找到第一个空行,等等.

Depending on what your file looks like you might want to analyze the line, e.g. look at the first character whether it is a comment sign, or ignore everything until you find the first empty line, etc.

有时候,硬编码魔术"数字并不是一个好主意.如果文件格式需要更改为包含3个标题行怎么办?

Sometimes, hardcoding "magic" numbers isn't such a good idea. What if the file format needs to be changed to contain 3 header lines?

正如其他答案所表明的那样:没有什么可以阻止您使用已阅读的台词来做自己想做的事,因此,当然,您也可以忽略它.

As the other answers demonstrate: Nothing keeps you from doing what you ever want with a line you have read, so of course, you can ignore it, too.

编辑,现在您已经提供了文件示例:对于您的情况,我肯定会 方法.如果某天该SQL语句应包含另一个字段,或者它出现在一行而不是8行上,该怎么办?

Edit, now that you've provided an example of your file: For your case I'd definitely not use the hardcoded numbers approach. What if some day the SQL statement should contain another field, or if it appears on one instead of 8 lines?

我的建议:一次读取整个字符串,然后进行分析.最安全的方法是使用语法,但是如果您假定SQL语句永远不会变得更加复杂,您可以使用正则表达式(比使用行号等要好得多):

My suggestion: Read in the whole string at once, then analyze it. Safest way would be to use a grammar, but if you presume the SQL statement is never going to be more complicated, you can use a regular expression (still much better than using line numbers etc.):

string content = File.ReadAllText(filename);
Regex r = new Regex(@"CREATE TABLE [^\(]+\((.*)\) ON");
string whatYouWant = r.Match(content).Groups[0].Value;

这篇关于有什么办法可以忽略文本文件中某些行的读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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