Compare two csv file using in C#
up vote
1
down vote
favorite
I need to find out difference between two csv files programatically.
Is there any way to find out the difference without using any loops?
Please help me.
c#
add a comment |
up vote
1
down vote
favorite
I need to find out difference between two csv files programatically.
Is there any way to find out the difference without using any loops?
Please help me.
c#
What format do you need the differences to be in?
– Oded
Mar 1 '11 at 12:16
If you need differnce in data, than yes you need. If you just need to know if files are different than no
– Stecya
Mar 1 '11 at 12:16
1
Read both csv file into list and then apply Linq set operation
– santosh singh
Mar 1 '11 at 12:16
without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
– Terry
Mar 1 '11 at 12:17
You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
– Security Hound
Mar 1 '11 at 12:17
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need to find out difference between two csv files programatically.
Is there any way to find out the difference without using any loops?
Please help me.
c#
I need to find out difference between two csv files programatically.
Is there any way to find out the difference without using any loops?
Please help me.
c#
c#
asked Mar 1 '11 at 12:15
Vishnu
4752824
4752824
What format do you need the differences to be in?
– Oded
Mar 1 '11 at 12:16
If you need differnce in data, than yes you need. If you just need to know if files are different than no
– Stecya
Mar 1 '11 at 12:16
1
Read both csv file into list and then apply Linq set operation
– santosh singh
Mar 1 '11 at 12:16
without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
– Terry
Mar 1 '11 at 12:17
You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
– Security Hound
Mar 1 '11 at 12:17
add a comment |
What format do you need the differences to be in?
– Oded
Mar 1 '11 at 12:16
If you need differnce in data, than yes you need. If you just need to know if files are different than no
– Stecya
Mar 1 '11 at 12:16
1
Read both csv file into list and then apply Linq set operation
– santosh singh
Mar 1 '11 at 12:16
without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
– Terry
Mar 1 '11 at 12:17
You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
– Security Hound
Mar 1 '11 at 12:17
What format do you need the differences to be in?
– Oded
Mar 1 '11 at 12:16
What format do you need the differences to be in?
– Oded
Mar 1 '11 at 12:16
If you need differnce in data, than yes you need. If you just need to know if files are different than no
– Stecya
Mar 1 '11 at 12:16
If you need differnce in data, than yes you need. If you just need to know if files are different than no
– Stecya
Mar 1 '11 at 12:16
1
1
Read both csv file into list and then apply Linq set operation
– santosh singh
Mar 1 '11 at 12:16
Read both csv file into list and then apply Linq set operation
– santosh singh
Mar 1 '11 at 12:16
without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
– Terry
Mar 1 '11 at 12:17
without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
– Terry
Mar 1 '11 at 12:17
You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
– Security Hound
Mar 1 '11 at 12:17
You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
– Security Hound
Mar 1 '11 at 12:17
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
Have you looked at the following links ?
If not, then you should.
C# - Comparing two CSV Files and giving an output- Comparing 2 CSV files in C# advice?
herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
– Vishnu
Mar 2 '11 at 7:13
@Anish : Welcome :-)
– sgokhales
Mar 2 '11 at 7:47
add a comment |
up vote
3
down vote
How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream
call Getbytes
and then pass the two arrays into Enumerable.SequenceEqual
private static byte GetFileHash(string filename)
{
using(var stream = new FileStream(filename, FileMode.Open))
{
var md5Hasher = new MD5CryptoServiceProvider();
return md5Hasher.ComputeHash(stream);
}
}
var file1hash = GetFileHash("file1.ext");
var file2hash = GetFileHash("file2.ext");
var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);
Now there are loops being used, just not by you.
add a comment |
up vote
0
down vote
No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?
Anyway, assuming both CSV are sorted by an ID column:
- Try splitting the files into rows
- In a loop
- Split each row as
List<string>
or as array - Compare the lists of both files (ignore trailing empty columns etc.)
- When differences in data columns were found Save a new row containing the differences into a
List<List<string>>
- When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file
- Split each row as
add a comment |
up vote
0
down vote
Check the code below to CompareTwoCSVFile and report in another .csv file
class CompareTwoCSVFile
{
public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
{
var csv = new StringBuilder();
string fileContentsOne = File.ReadAllLines(filePathOne);
string fileContentsTwo = File.ReadAllLines(filePathTwo);
if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
return false;
string columnshead1 = fileContentsOne[0].Split(new char { ';' });
List<string> heading1 = new List<string>();
Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
string headingsplit = columnshead1[0].Split(',');
for (int i=0;i< headingsplit.Length;i++)
{
heading1.Add(headingsplit[i]);
}
var newLine = "";
newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
csv.AppendLine(newLine);
for (int i = 0; i < fileContentsOne.Length-1; ++i)
{
string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });
string cellOne = columnsOne[0].Split(',');
string cellTwo = columnsTwo[0].Split(',');
dict1[i] = new Dictionary<string, string>();
dict2[i] = new Dictionary<string, string>();
for(int j=0;j< headingsplit.Length;j++)
{
dict1[i].Add(heading1[j],cellOne[j]);
}
for (int j = 0; j < headingsplit.Length; j++)
{
dict2[i].Add(heading1[j], cellTwo[j]);
}
foreach (KeyValuePair<string, string> entry in dict1[i])
{
if (dict2[i][entry.Key].Equals(entry.Value)!=true)
{
Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
csv.AppendLine(newLine);
}
}
}
File.WriteAllText("D:\Errorlist.csv", csv.ToString());
return true;
}
}
New contributor
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Have you looked at the following links ?
If not, then you should.
C# - Comparing two CSV Files and giving an output- Comparing 2 CSV files in C# advice?
herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
– Vishnu
Mar 2 '11 at 7:13
@Anish : Welcome :-)
– sgokhales
Mar 2 '11 at 7:47
add a comment |
up vote
2
down vote
accepted
Have you looked at the following links ?
If not, then you should.
C# - Comparing two CSV Files and giving an output- Comparing 2 CSV files in C# advice?
herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
– Vishnu
Mar 2 '11 at 7:13
@Anish : Welcome :-)
– sgokhales
Mar 2 '11 at 7:47
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Have you looked at the following links ?
If not, then you should.
C# - Comparing two CSV Files and giving an output- Comparing 2 CSV files in C# advice?
Have you looked at the following links ?
If not, then you should.
C# - Comparing two CSV Files and giving an output- Comparing 2 CSV files in C# advice?
edited May 23 '17 at 12:07
Community♦
11
11
answered Mar 1 '11 at 12:27
sgokhales
35k26105138
35k26105138
herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
– Vishnu
Mar 2 '11 at 7:13
@Anish : Welcome :-)
– sgokhales
Mar 2 '11 at 7:47
add a comment |
herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
– Vishnu
Mar 2 '11 at 7:13
@Anish : Welcome :-)
– sgokhales
Mar 2 '11 at 7:47
herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
– Vishnu
Mar 2 '11 at 7:13
herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
– Vishnu
Mar 2 '11 at 7:13
@Anish : Welcome :-)
– sgokhales
Mar 2 '11 at 7:47
@Anish : Welcome :-)
– sgokhales
Mar 2 '11 at 7:47
add a comment |
up vote
3
down vote
How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream
call Getbytes
and then pass the two arrays into Enumerable.SequenceEqual
private static byte GetFileHash(string filename)
{
using(var stream = new FileStream(filename, FileMode.Open))
{
var md5Hasher = new MD5CryptoServiceProvider();
return md5Hasher.ComputeHash(stream);
}
}
var file1hash = GetFileHash("file1.ext");
var file2hash = GetFileHash("file2.ext");
var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);
Now there are loops being used, just not by you.
add a comment |
up vote
3
down vote
How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream
call Getbytes
and then pass the two arrays into Enumerable.SequenceEqual
private static byte GetFileHash(string filename)
{
using(var stream = new FileStream(filename, FileMode.Open))
{
var md5Hasher = new MD5CryptoServiceProvider();
return md5Hasher.ComputeHash(stream);
}
}
var file1hash = GetFileHash("file1.ext");
var file2hash = GetFileHash("file2.ext");
var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);
Now there are loops being used, just not by you.
add a comment |
up vote
3
down vote
up vote
3
down vote
How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream
call Getbytes
and then pass the two arrays into Enumerable.SequenceEqual
private static byte GetFileHash(string filename)
{
using(var stream = new FileStream(filename, FileMode.Open))
{
var md5Hasher = new MD5CryptoServiceProvider();
return md5Hasher.ComputeHash(stream);
}
}
var file1hash = GetFileHash("file1.ext");
var file2hash = GetFileHash("file2.ext");
var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);
Now there are loops being used, just not by you.
How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream
call Getbytes
and then pass the two arrays into Enumerable.SequenceEqual
private static byte GetFileHash(string filename)
{
using(var stream = new FileStream(filename, FileMode.Open))
{
var md5Hasher = new MD5CryptoServiceProvider();
return md5Hasher.ComputeHash(stream);
}
}
var file1hash = GetFileHash("file1.ext");
var file2hash = GetFileHash("file2.ext");
var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);
Now there are loops being used, just not by you.
answered Mar 1 '11 at 12:40
ilivewithian
12.1k1676149
12.1k1676149
add a comment |
add a comment |
up vote
0
down vote
No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?
Anyway, assuming both CSV are sorted by an ID column:
- Try splitting the files into rows
- In a loop
- Split each row as
List<string>
or as array - Compare the lists of both files (ignore trailing empty columns etc.)
- When differences in data columns were found Save a new row containing the differences into a
List<List<string>>
- When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file
- Split each row as
add a comment |
up vote
0
down vote
No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?
Anyway, assuming both CSV are sorted by an ID column:
- Try splitting the files into rows
- In a loop
- Split each row as
List<string>
or as array - Compare the lists of both files (ignore trailing empty columns etc.)
- When differences in data columns were found Save a new row containing the differences into a
List<List<string>>
- When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file
- Split each row as
add a comment |
up vote
0
down vote
up vote
0
down vote
No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?
Anyway, assuming both CSV are sorted by an ID column:
- Try splitting the files into rows
- In a loop
- Split each row as
List<string>
or as array - Compare the lists of both files (ignore trailing empty columns etc.)
- When differences in data columns were found Save a new row containing the differences into a
List<List<string>>
- When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file
- Split each row as
No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?
Anyway, assuming both CSV are sorted by an ID column:
- Try splitting the files into rows
- In a loop
- Split each row as
List<string>
or as array - Compare the lists of both files (ignore trailing empty columns etc.)
- When differences in data columns were found Save a new row containing the differences into a
List<List<string>>
- When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file
- Split each row as
edited Mar 1 '11 at 12:27
answered Mar 1 '11 at 12:20
matthias.lukaszek
1,5671729
1,5671729
add a comment |
add a comment |
up vote
0
down vote
Check the code below to CompareTwoCSVFile and report in another .csv file
class CompareTwoCSVFile
{
public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
{
var csv = new StringBuilder();
string fileContentsOne = File.ReadAllLines(filePathOne);
string fileContentsTwo = File.ReadAllLines(filePathTwo);
if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
return false;
string columnshead1 = fileContentsOne[0].Split(new char { ';' });
List<string> heading1 = new List<string>();
Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
string headingsplit = columnshead1[0].Split(',');
for (int i=0;i< headingsplit.Length;i++)
{
heading1.Add(headingsplit[i]);
}
var newLine = "";
newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
csv.AppendLine(newLine);
for (int i = 0; i < fileContentsOne.Length-1; ++i)
{
string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });
string cellOne = columnsOne[0].Split(',');
string cellTwo = columnsTwo[0].Split(',');
dict1[i] = new Dictionary<string, string>();
dict2[i] = new Dictionary<string, string>();
for(int j=0;j< headingsplit.Length;j++)
{
dict1[i].Add(heading1[j],cellOne[j]);
}
for (int j = 0; j < headingsplit.Length; j++)
{
dict2[i].Add(heading1[j], cellTwo[j]);
}
foreach (KeyValuePair<string, string> entry in dict1[i])
{
if (dict2[i][entry.Key].Equals(entry.Value)!=true)
{
Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
csv.AppendLine(newLine);
}
}
}
File.WriteAllText("D:\Errorlist.csv", csv.ToString());
return true;
}
}
New contributor
add a comment |
up vote
0
down vote
Check the code below to CompareTwoCSVFile and report in another .csv file
class CompareTwoCSVFile
{
public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
{
var csv = new StringBuilder();
string fileContentsOne = File.ReadAllLines(filePathOne);
string fileContentsTwo = File.ReadAllLines(filePathTwo);
if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
return false;
string columnshead1 = fileContentsOne[0].Split(new char { ';' });
List<string> heading1 = new List<string>();
Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
string headingsplit = columnshead1[0].Split(',');
for (int i=0;i< headingsplit.Length;i++)
{
heading1.Add(headingsplit[i]);
}
var newLine = "";
newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
csv.AppendLine(newLine);
for (int i = 0; i < fileContentsOne.Length-1; ++i)
{
string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });
string cellOne = columnsOne[0].Split(',');
string cellTwo = columnsTwo[0].Split(',');
dict1[i] = new Dictionary<string, string>();
dict2[i] = new Dictionary<string, string>();
for(int j=0;j< headingsplit.Length;j++)
{
dict1[i].Add(heading1[j],cellOne[j]);
}
for (int j = 0; j < headingsplit.Length; j++)
{
dict2[i].Add(heading1[j], cellTwo[j]);
}
foreach (KeyValuePair<string, string> entry in dict1[i])
{
if (dict2[i][entry.Key].Equals(entry.Value)!=true)
{
Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
csv.AppendLine(newLine);
}
}
}
File.WriteAllText("D:\Errorlist.csv", csv.ToString());
return true;
}
}
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
Check the code below to CompareTwoCSVFile and report in another .csv file
class CompareTwoCSVFile
{
public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
{
var csv = new StringBuilder();
string fileContentsOne = File.ReadAllLines(filePathOne);
string fileContentsTwo = File.ReadAllLines(filePathTwo);
if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
return false;
string columnshead1 = fileContentsOne[0].Split(new char { ';' });
List<string> heading1 = new List<string>();
Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
string headingsplit = columnshead1[0].Split(',');
for (int i=0;i< headingsplit.Length;i++)
{
heading1.Add(headingsplit[i]);
}
var newLine = "";
newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
csv.AppendLine(newLine);
for (int i = 0; i < fileContentsOne.Length-1; ++i)
{
string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });
string cellOne = columnsOne[0].Split(',');
string cellTwo = columnsTwo[0].Split(',');
dict1[i] = new Dictionary<string, string>();
dict2[i] = new Dictionary<string, string>();
for(int j=0;j< headingsplit.Length;j++)
{
dict1[i].Add(heading1[j],cellOne[j]);
}
for (int j = 0; j < headingsplit.Length; j++)
{
dict2[i].Add(heading1[j], cellTwo[j]);
}
foreach (KeyValuePair<string, string> entry in dict1[i])
{
if (dict2[i][entry.Key].Equals(entry.Value)!=true)
{
Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
csv.AppendLine(newLine);
}
}
}
File.WriteAllText("D:\Errorlist.csv", csv.ToString());
return true;
}
}
New contributor
Check the code below to CompareTwoCSVFile and report in another .csv file
class CompareTwoCSVFile
{
public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
{
var csv = new StringBuilder();
string fileContentsOne = File.ReadAllLines(filePathOne);
string fileContentsTwo = File.ReadAllLines(filePathTwo);
if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
return false;
string columnshead1 = fileContentsOne[0].Split(new char { ';' });
List<string> heading1 = new List<string>();
Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
string headingsplit = columnshead1[0].Split(',');
for (int i=0;i< headingsplit.Length;i++)
{
heading1.Add(headingsplit[i]);
}
var newLine = "";
newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
csv.AppendLine(newLine);
for (int i = 0; i < fileContentsOne.Length-1; ++i)
{
string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });
string cellOne = columnsOne[0].Split(',');
string cellTwo = columnsTwo[0].Split(',');
dict1[i] = new Dictionary<string, string>();
dict2[i] = new Dictionary<string, string>();
for(int j=0;j< headingsplit.Length;j++)
{
dict1[i].Add(heading1[j],cellOne[j]);
}
for (int j = 0; j < headingsplit.Length; j++)
{
dict2[i].Add(heading1[j], cellTwo[j]);
}
foreach (KeyValuePair<string, string> entry in dict1[i])
{
if (dict2[i][entry.Key].Equals(entry.Value)!=true)
{
Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
csv.AppendLine(newLine);
}
}
}
File.WriteAllText("D:\Errorlist.csv", csv.ToString());
return true;
}
}
New contributor
edited Nov 22 at 5:06
New contributor
answered Nov 21 at 3:10
Naveen_mike
12
12
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5154370%2fcompare-two-csv-file-using-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What format do you need the differences to be in?
– Oded
Mar 1 '11 at 12:16
If you need differnce in data, than yes you need. If you just need to know if files are different than no
– Stecya
Mar 1 '11 at 12:16
1
Read both csv file into list and then apply Linq set operation
– santosh singh
Mar 1 '11 at 12:16
without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
– Terry
Mar 1 '11 at 12:17
You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
– Security Hound
Mar 1 '11 at 12:17