简体   繁体   中英

Skipping header when reading CSV

I want to be able to skip the header when reading and writing to csv

Here is the code I have so far

  try
        {
            using (StreamWriter sw = new StreamWriter(writefilename))// StreamWriter is used to write a text file
            using (StreamReader sr = new StreamReader(readfilename)) // StreamReader is used to read a text file
            {
                //Writing headings to file
                heading = "NI Number,Surname,Forename,Employee,Employer(gross),Contribution Status,Contribution Status Date";
                sw.WriteLine(heading);


                while ((txtline = sr.ReadLine()) != null)  // Reads one line into the variable txtline
                {



                    //spiliting the file into columns if there is something in it.
                    //oldcolumns = txtline.Split(',');

                    oldcolumns = Regex.Split(txtline, ",");

                    //spliting old columns[0] where there is a space and putting it in the names array.
                    //names = Regex.Split(oldcolumns[0],",");
                  string []  names = oldcolumns[0].Split(' ');
                  var result = string.Join(" ", oldcolumns[0].Split(' ').Skip(1));




                    //writing the oldcolumns data into the newcolumns.
                    newcolumns[0] = oldcolumns[1];
                    newcolumns[1] = result;
                    newcolumns[2] = names[0];
                    newcolumns[3] = oldcolumns[9];
                    newcolumns[4] = oldcolumns[11];
                    newcolumns[5] = "";
                    newcolumns[6] = "";


                    //using loop to run through all the columns
                    csvline = "";


                    for (int i = 0; i < 7; i++)
                    {
                        csvline = csvline + "\"" + newcolumns[i].Replace("\"", "") + "\",";
                    }

                    //writing to file.
                    sw.WriteLine(csvline);
                }

I understand that if I wanted to skip a column I could do this in the for loop but I dont know how to skip a row.

Just count the rows & ignore it:

for ( int LineNo = 1; ; LineNo++)
{
  ReadLine
  if (LineNo == 1)
    continue
}

(pseudo-code before anyone comments :-) )

If all you want to do is discard the first line of a file, simply call ReadLine() and discard the result before further processing:

// Discard the header row
sr.ReadLine()

// Do further processing
while ((txtline = sr.ReadLine()) != null)
{
}

Please note that one line does not necessarily mean one row in CSV. Use a library to read CSV.

The continue keyword will jump to the next iteration of your loop.

while ((txtline = sr.ReadLine()) != null)
{
   if (txtline == heading) continue; //Ignore headers

   //do standard processing
}

This code assumes you have the same headers in both files, if not, replace heading with the headers of the file you are reading from.

Whilst the answers are all valid, I would use a library every time for parsing CSV files.

Not only does it provide things out of the box to skip header rows, but it will also deal with pesky things like commas inside double quotes, etc.

CSVHelper is my go-to CSV parsing library.

It also gives you really nice syntax, like this, quoted from the above link:

var csv = new CsvReader( textReader );
var records = csv.GetRecords<MyClass>();

Much easier to read ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM