开发者

is there a way to remove the first line from a StreamReader

i have a streamreader which loads up a csvfile, the issue is that there is one extra row at the top of the data that i want to ignore.

I am using the CSVR开发者_Python百科eader project which is great but

 csv.GetFieldHeaders(); 

always looks at the first row. Instead of going into the CSVReader source code and changing it, i thought it might be easier if i can "strip" out this first row from the streamreader itself before injecting it into the csvReader. Is this possible?


Just read a line from the SR before you pass it to the CSVReader constructor. For example:

using (var sr = new StreamReader("data.csv")) {
    sr.ReadLine();
    using (var csv = new CsvReader(sr, true)) {
        // etc..
    }
}


The simplest way would just be to call ReadLine() yourself before you pass the reader on to the CSVReader.


StreamReader is a virtual class, so you could create an inherited class that delegates all important functionality to some underlying StreamReader, with the exception that it skips the first line of text (or you could inherit from the TextReader type). If the CSVReader takes the StreamReader or TextReader as an argument, this should work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜