我正在尝试比较CSV文件的两个特定部分。
JobStart.csv
test1;test2;test3;test4;Val2Compare2;test5;test6;test7....
Datafile.csv
line 1 Val2Compare1; data1;data2;data3;data4;data5;data6
line 2 Val2Compare2; data1;data2;data3;data4;data5;data6
line 3 Val2Compare3; data1;data2;data3;data4;data5;data6
我想做的是:
比较值:JobStart.csv Val2Compare2到
Datafile.csv Val2Compare1
Val2Compare2
Val2Compare3
...
当我得到哪一行时(在这种情况下为line2)。然后我想像这样组合JobStat.csv和Datafile.csv:
NEW JOBSTART.CSV
test1;test2;test3;test4;Val2Compare2;test5;test6;test7; data1;data2;data3;data4;data5
我试着看看:
Compare two csv file using in C#
Comparing specific columns of two csv files
Compare 2 csv files and replace updated values
但是得不到多少帮助。
答案 0 :(得分:2)
试试这个:
var jobStartLine = File.OpenText(@"JobStart.csv").ReadLine();
var comparisonField = jobStartLine.Split(';')[4];
foreach (var line in File.ReadAllLines(@"Datafile.csv"))
{
var fields = line.Split(new char[] {';'}, 2);
if (comparisonField == fields[0])
{
File.WriteAllLines(@"NewJobStart.csv", new string[] { jobStartLine + ";" + fields[1] });
break;
}
}