Chris Vessels
Chris Vessels

Reputation: 5

C# While loop skipping virst line when parsing csv

I have a simple CSV file I need to parse that has variable column lengths. CSV File for reference.

I'm attempting to write some conditions to parse each line and store into a discreet string based off what value the first column contains in each line.

The first issue that I'm running into is that my loop seems to start reading at row 2. Is this because TextFieldParser assumes a header?

The second issue is that my if statements don't seem to be evaluating correctly. If I shift the order of the rows within the file, my first if in the order is supposed to parse the "Lighting" rows (regardless of which rows in the file contain "Lighting").

Just as a note, I am working in a .net 3.5 environment

Here's what I've got so far:

namespace CSV_Handler_Console
{
    public class Program
    {

        public static void Main(string[] args)
        {
            //Console.WriteLine("Press Enter");

            string filePath  = "C:\\Users\\chris\\Desktop\\ConfigFile.csv";



            string subLight = "Lighting";
            string subPin = "PIN";
            string subProc = "Processor";
            string subFab = "Fabuloso";
            string subQuirky = "Quirky";

            //string[] fields = csvParser.ReadFields();
            string category = string.Empty;
            string index = string.Empty;
            string load1 = string.Empty;
            string load2 = string.Empty;
            string load3 = string.Empty;
            string load4 = string.Empty;
            string value = string.Empty;

            string light1 = string.Empty;
            string light2 = string.Empty;
            string light3 = string.Empty;
            string pin = string.Empty;
            string processor1 = string.Empty;
            string processor2 = string.Empty;
            string processor3 = string.Empty;
            string processor4 = string.Empty;
            string processor5 = string.Empty;
            string processor6 = string.Empty;
            string processor7 = string.Empty;
            string processor8 = string.Empty;
            string display1 = string.Empty;
            string display2 = string.Empty;
            string display3 = string.Empty;
            string display4 = string.Empty;
            string display5 = string.Empty;
            string display6 = string.Empty;
            string display7 = string.Empty;
            string display8 = string.Empty;


            var path = String.Format("{0}", filePath);

            using (TextFieldParser csvParser = new TextFieldParser(path))
            {

                csvParser.SetDelimiters(new string[] { "," });
                //csvParser.HasFieldsEnclosedInQuotes = false;

                string row = csvParser.ReadLine();

                while (!csvParser.EndOfData)
                {                   
                        if (row.Contains(subLight))
                        {
                            string[] fields = csvParser.ReadFields();

                            category = fields[0];
                            index = fields[1];
                            load1 = fields[2];
                            load2 = fields[3];
                            load3 = fields[4];
                            load4 = fields[5];

                            if(index.Contains("1"))
                            {
                                light1 = row;
                            }
                            else if (index.Contains("2"))
                            {
                                light2 = row;
                            }
                            else if (index.Contains("3"))
                            {
                                light3 = row;
                            }

                            string rowData = string.Format("{0},{1},{2},{3},{4},{5}", category, index, load1, load2, load3, load4);
                            Console.WriteLine(rowData);
                            //Console.ReadLine();
                        }
                        else if (row.Contains(subPin))
                        {
                            string[] fields = csvParser.ReadFields();

                            category = fields[0];
                            index = fields[1];
                            value = fields[2];
                            string rowData = string.Format("{0},{1},{2}", category, index, value);
                            Console.WriteLine(rowData);
                        }
                        else if (row.Contains(subProc))
                        {
                            string[] fields = csvParser.ReadFields();


                            category = fields[0];
                            index = fields[1];
                            value = fields[2];

                            if (index.Contains("A"))
                            {
                                processor1 = row;
                            }
                            else if (index.Contains("B"))
                            {
                                processor2 = row;
                            }
                            else if (index.Contains("C"))
                            {
                                processor3 = row;
                            }
                            else if (index.Contains("D"))
                            {
                                processor4 = row;
                            }
                            else if (index.Contains("E"))
                            {
                                processor5 = row;
                            }
                            else if (index.Contains("F"))
                            {
                                processor6 = row;
                            }
                            else if (index.Contains("G"))
                            {
                                processor7 = row;
                            }
                            else if (index.Contains("H"))
                            {
                                processor8 = row;
                            }
                            string rowData = string.Format("{0},{1},{2}", category, index, value);
                            Console.WriteLine(rowData);
                        }
                        else if (row.Contains(subQuirky) || row.Contains(subFab))
                        {
                            string[] fields = csvParser.ReadFields();


                            category = fields[0];
                            index = fields[1];
                            value = fields[2];

                            if (index.Contains("A"))
                            {
                                display1 = row;
                            }
                            else if (index.Contains("B"))
                            {
                                display2 = row;
                            }
                            else if (index.Contains("C"))
                            {
                                display3 = row;
                            }
                            else if (index.Contains("D"))
                            {
                                display4 = row;
                            }
                            else if (index.Contains("E"))
                            {
                                display5 = row;
                            }
                            else if (index.Contains("F"))
                            {
                                display6 = row;
                            }
                            else if (index.Contains("G"))
                            {
                                display7 = row;
                            }
                            else if (index.Contains("H"))
                            {
                                display8 = row;
                            }
                            string rowData = string.Format("{0},{1},{2}", category, index, value);
                            Console.WriteLine(rowData);
                        }
                        else
                        {
                            Console.WriteLine("No Match Found");
                        }
                    }

                Console.ReadLine();

                }
        }

}  

Any guidance would be appreciated.

Upvotes: 0

Views: 246

Answers (1)

bradleyfitz
bradleyfitz

Reputation: 691

It's your implementation. You first grab the first row with ReadLine, which advances the cursor to the next line. Then if the row contains your search property, you do a ReadFields, which is the 2nd line of the document.

If your data is always guaranteed to have the category, you could just use ReadFields and compare against the first element. You could look at PeekChars if you want to look at the content of the current row without advancing the cursor.

TextFieldParser.ReadLine: Returns the current line as a string and advances the cursor to the next line.

TextFieldParser.ReadFields: Reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.

TextFieldParser.PeekChars: Reads the specified number of characters without advancing the cursor.

Upvotes: 2

Related Questions