I need to build a method to enhance one csv file with values from another. This method would need to:
- take the "original" csv file
- for each row from its column 0, look up for a matching record in column 0 of "enhancement" csv file
- If there is a match, then for this row the record in column 1 of "original" file will get overwritten by corresponding record in column 1 of the "enhancement" file
I 'm trying the below pattern, which seems workable - but it is so slow that I'm not even able to check it. The size of the files should not be an issue, because one is 1MB, another 2MB, but I'm definitely taking some wrong assumptions to do this efficiently. What would be a better way of doing this?
public static string[] LoadReadyCsv()
{
string[] scr = System.IO.File.ReadAllLines(@Path...CsvScr);
string[] aws = System.IO.File.ReadAllLines(@Path...CsvAws);
Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
foreach (var s in scr)
{
string[] fieldsScr = CSVParser.Split(s);
foreach (var a in aws)
{
string[] fieldsAws = CSVParser.Split(a);
if (fieldsScr[0] == fieldsAws[0])
{
fieldsScr[1] = fieldsAws[1];
}
}
}
return scr;
}
EDIT: I add an example below, as requested
"Original file"
ean, skunum, prodname
111, empty, bread
222, empty, cheese
"Enhancement file"
ean, skunum, prodname
111, 555, foo
333, 444, foo
New "Original file"
ean,skunum,prodname
111, 555, bread
222, empty, cheese