0

I have csvFile's structured like so:

val1,val2,val3
1,2,4
.
.
val1,val2,val3
3,7,8

and

val1,val2,val3
1,2,4
.
.
val1,val2,val3
3,7,8
.
.
val1,val2,val3
11,5,9

For this, the csv headers all have the same name.

With that, my question is, how would I skip all the way down to the very last table/dataset? So to get tables:

//first csvFile
val1,val2,val3
3,7,8

//second csvFile
val1,val2,val3
11,5,9

What I've done so far is implement

 for (var i = 0; i < 7; i++)//skip number of lines
  {
 csv.Read();//function here skips line to get to real header
  }     
 csv.Read();
 csv.ReadHeader();

With this, I can skip lines all the way down, but that is only provided that I know the spacing to get to the line that I want to, so it is very file specific. How would it be implemented so you can always get the last Header values for a csvFile without taking in consideration the number of lines?

I am also using the open source library, CsvHelper

  • how do you want output? string? or write to a file? – Vivek Nuna Jun 14 '20 at 10:23
  • As a write to file please – thesenate42069 Jun 14 '20 at 10:28
  • but in the question you have mentioned two sets – Vivek Nuna Jun 14 '20 at 10:34
  • oh yea, sorry if its not clear, so I want to, regardless of the number of Headers in a csvFile, always skip down to the last one. So in the first file, there are 2 header rows, and in the second file, there are 3 header rows. My goal is to make where it will always go down to the last header row, regardless of how many headers there are – thesenate42069 Jun 14 '20 at 10:37
  • For this, I’m using the open source library called CsvHelper to parse a csvFile. In a csvFile there are headers which in this file are Val1,Val2,Val3. With that they all have values under them. With that, I am trying to always get down to the last headers and it’s values, which in here is shown above my for loop statement – thesenate42069 Jun 14 '20 at 10:55

1 Answers1

1

CsvHelper will read from a Reader. Readers can read from strings. We can thus preprocess the file like this:

  • Make a stringbuilder
  • Read the file line by line, adding to the stringbuilder
  • Every time we encounter a set of headers, clear the stringbuilder
  • Use CsvHelper to parse the stringbuilder contents- logically it can only contain the last set of csv data because we keep clearing it
var sb as new StringBuilder();
foreach(var line in File.ReadLines("path"){
    if(line == "val1,val2,val3")
        sb.Clear();

    sb.AppendLine(line);
}

using (var reader = new StringReader(sb.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{    
    var records = csv.GetRecords<...>();
}

If the val1,val2,val3 will change their order, presentation, spaces etc then you could make the headers into an array of strings and then ask whether a line contained all the headers:

var headers = "val1 val2 val3".Split();

//and in the loop
if(headers.All(h => line.Contains(h)))
   ...
Caius Jard
  • 72,509
  • 5
  • 49
  • 80