chrisd
chrisd

Reputation: 1

Comparing and creating a CSV file in C#

I am creating a CSV file using C# this contains 200 headers. I'm getting the data from another CSV file which contains 150 headers. My problem is how I'm going to place the data according to its header. For instance I'm giving and example below.

The CSV file which will be created with C#:

Name, Surname, Locality, DateOfbirth, Age
Joe,  Smith,                          60
Sam,  Brown,                          20

The CSV getting the data from

Name, Surname, Age
Joe,  Smith,   60
Sam,  Brown,   20

This is a sample code (the actual files contains 150 header, and the new CSV file contains 200 headers)

string[] lines = System.IO.File.ReadAllLines(fileUrl);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileUrl))
{
    foreach (string line in lines)
    {
        if (line == lines[0])
        {   
            //Changing the header of the first file
            file.WriteLine("Name, Surname, Locality, DateOfBirth, Age");
        }
        else
        {
            string[] values = line.Split(',');
            file.WriteLine(string.Format("{0},{1},{2},{3},{4}",
                                values[0], values[1], values[2], values[3], values[4]));
        } //exception being thrown here since the array is out of range
    }
}

Upvotes: 0

Views: 820

Answers (2)

Demir
Demir

Reputation: 1837

Read the data from file that contains 3 columns. Then read Locality and DateOfBirth values from that another file. Clear the first file and then write them all in a new csv file.

    public static List<string[]> Parse(string Path)
    {
        List<string[]> Parsed = new List<string[]>();

        using (StreamReader Reader = new StreamReader(Path))
        {
            string Line;
            char Seperator = ',';

            while ((Line = Reader.ReadLine()) != null)
            {
                if (Line.Trim().StartsWith("//")) continue;
                if (string.IsNullOrWhiteSpace(Line)) continue;

                string[] Data = Line.Split(Seperator);
                Parsed.Add(Data);
            }
        }

        return Parsed;
    } 

You can use the method above to read from CSV file. Imagine that you read first file and you get List and string array has 3 values.

Read second file and get other values. For each value in first List find the corresponding item in the second List. Then use these two string array lists to write into a csv file.

List<string[]> File1 = Parse("File1Path");
List<string[]> File2 = Parse("File2Path");

using (System.IO.StreamWriter file = new System.IO.StreamWriter(OutputFile))
{
   // write header first:    
   file.WriteLine("Name, Surname, Locality, DateOfBirth, Age");

   foreach (string line in File1)
   {
     var found = File2.Where(x => x[0] == line[0]).FirstOrDefault();         
     if(null == found) continue;

     file.WriteLine(string.Format("{0},{1},{2},{3},{4}",
                                line[0], line[1], found[3], found[4], line[2]));
    }
}

Upvotes: 0

David Arno
David Arno

Reputation: 43254

You are reading just three columns from the input file, yet are trying to write out five columns. So values[3] and values[4] will be out of range.

I'm puzzled as to where you are expecting to get Location and DateOfBirth from. Wherever it is, it won't be in your values array.

Upvotes: 2

Related Questions