0

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
    }
}
3
  • You're doing line.Split(','); but I don't see any commas it the 'actual csv file'? Commented Oct 24, 2013 at 11:18
  • I need to keep the data from the original file to the new one. @DavidS. the above tables are just used for graphical purpose so that people don't get confused with the data Commented Oct 24, 2013 at 11:21
  • Are the CSV's rows in the same order on both files? and do you always know the structure? Commented Oct 24, 2013 at 12:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting it from another CSV file
@ChristianDebono What is in the second CSV file? Please include details of that in your question.
0

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]));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.