Ashton
Ashton

Reputation: 363

Why is my logic not adding the quotes back to the CSV File for the columns after the First one?

I am using hashmaps to store CSVFile1 and compare it to CSVFile2. CSVFile1 has a column of customers to remove while CSVFile2 has all of the completed data (which has more then 1 column and that is why I am only comparing dataArray2[0]). Once I compare I only rewrite the files that are NOT in the CSVFile1 to a file called CSVFileFinal.

The issue is that some of the data has commas inside of fields and when I read the file it take them out once I try and rewrite. My goal is to rewrite the data with the quotes back around the commas in the fields which I "split" at.

I have managed to get the First column (the most important one) to split and replace the quotes perfectly! But for whatever reason my second loop is messing up and not checking the columns after that correctly. I believe it is simply the placing of my code.

This is the logical statement/placement in question:

if (dataArray2[i].Contains(","))
{
    string x = "\"" + dataArray2[i] + "\"";
    record = x;
}

I know the logic works because if you look at my entire source code; the code posted works for the first column and works in the 2nd IF statement for If i == 0 meaning if it's the 1st columns. But how or where do I place that to work for the others afterward?

Can anyone help figure out where to place this for the following columns? It should be a simple copy and pasting of the code I have but I've been unable to figure it out. Thank you!

    int count = 0;
    while (!CSVFile2.EndOfData)
    {
        if (!badCustomers.ContainsKey(dataArray2[0]))
        {
            String record = null;
            for(int i = 0; i < dataArray2.Length; i++)
            {
                if(i == 0)
                {
                    if (dataArray2[i].Contains(","))
                    {
                        string x = "\"" + dataArray2[i] + "\"";
                        record = x;
                    }
                    else
                        record = dataArray2[i];
                } else
                {
                    record = record + "," + dataArray2[i];

                }
            }
            count++;
            if( count % 50 == 0)
            {
                Console.WriteLine(count);
            }
            output.WriteLine(record);
        }
        dataArray2 = CSVFile2.ReadFields();
    }

The above code works but does not consider the later columns after the first one to replace this quotes- this code is what should fix the ones after the first column but for whatever reason it does not. Why?

 if (!badCustomers.ContainsKey(dataArray2[0]))
                {
                    String record = null;
                    for(int i = 0; i < dataArray2.Length; i++)
                    {
                        if(i == 0)
                        {
                            if (dataArray2[i].Contains(","))
                            {
                                string x = "\"" + dataArray2[i] + "\"";
                                record = x;
                            }
                            else
                                record = dataArray2[i];
                        } else
                        {
                            if (dataArray2[i].Contains(","))
                            {
                                string x = "\"" + dataArray2[i] + "\"";
                                record = x;
                            }
                            else
                                record = record + "," + dataArray2[i];

                        }
                    }

Upvotes: 0

Views: 44

Answers (1)

Marshall Tigerus
Marshall Tigerus

Reputation: 3764

What I believe you are asking is for this:

int count = 0;
while (!CSVFile2.EndOfData)
{
    if (!badCustomers.ContainsKey(dataArray2[0]))
    {
        String record = String.Empty;
        for(int i = 0; i < dataArray2.Length; i++)
        {
            if (dataArray2[i].Contains(","))
            {
                 string x = "\"" + dataArray2[i] + "\"";
                 record += x;
            }
            else
            {
                 record += dataArray2[i];
            } 
            if (i < dataArray.Length -1)
            {
                  record += ",";
            }
        }
        count++;
        if( count % 50 == 0)
        {
            Console.WriteLine(count);
        }
        output.WriteLine(record);
    }
    dataArray2 = CSVFile2.ReadFields();
}

This loops through each line from the input, adding quotes around commas. You may have to tweak it to meet your final needs, but this should be a start.

The reason it was only ever doing it for your first column is the if(i == 0). This means the code would only ever execute for the first iteration (your first column).

Upvotes: 1

Related Questions