Home > Mobile >  Merge 2 CSV Files into one CSV File
Merge 2 CSV Files into one CSV File

Time:10-01

I need to merge two CSV Files into one.

As example: CSV_FileOne

Key;Dutch
ERROR_LicenseNotFound;Error de licensie kan niet gevonden worden.
ERROR_LicenseIsEmpty;Error de licensiefile is leeg.

CSV_FileTwo

Key;Deutsch
ERROR_LicenseNotFound;Fehler die Lizenz kann nicht gefunden werden.
ERROR_LicenseIsEmpty;Fehler die Lizenzdatei ist leer.

So the result shoud be:

Key;Dutch;Deutsch
ERROR_LicenseNotFound;Error de licensie kan niet gevonden worden.;Fehler die Lizenz kann nicht gefunden werden.
ERROR_LicenseIsEmpty;Error de licensiefile is leeg.;Fehler die Lizenzdatei ist leer.

I have to do this, because I am making a Program for a Dutch company.

I hop you can help me.

CodePudding user response:

you can load your files, that you want combined into memory and then create the output file on your own:

using System.IO;

// reading both csv files into memory
string[] left = File.ReadAllLines(pathToFile1);
string[] right = File.ReadAllLines(pathToFile2);

// opening stream   streamwriter
using (FileStream fs = new FileStream("", FileMode.Create))
using (StreamWriter sw = new StreamWriter(fs))
{
    for(int i = 0; i < left.Length; i  )
    {
        // in each line we take the entire line of left and append field 1 of right
        sw.WriteLine(left[i]   ";"   right[i].Split(';')[1];);
    }
    
}

please bear in mind: this is a quick and dirty solution. it expects that both files have the exact same order of keys. you could create a dictionary and associate the strings trough the key to get a more robust method but I'll leave some work for you to do

  • Related