简体   繁体   中英

Compare two csv files in C#

I want to compare two csv files and print the differences in a file. I currently use the code below to remove a row. Can I change this code so that it compares two csv files or is there a better way in c# to compare csv files?

  List<string> lines = new List<string>();
        using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(path)))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains(csvseperator))
                {
                     string[] split = line.Split(Convert.ToChar(scheidingsteken));

                    if (split[selectedRow] == value)
                    {

                    }
                    else
                    {
                        line = string.Join(csvseperator, split);
                        lines.Add(line);
                    }
                }

            }
        }

        using (StreamWriter writer = new StreamWriter(path, false))
        {
            foreach (string line in lines)
                writer.WriteLine(line);
        }
    }

If you only want to compare one column you can use this code:

                List<string> lines = new List<string>();
    List<string> lines2 = new List<string>();



    try
    {
        StreamReader reader = new StreamReader(System.IO.File.OpenRead(pad));
        StreamReader read = new StreamReader(System.IO.File.OpenRead(pad2));

        string line;
        string line2;

        //With this you can change the cells you want to compair
        int comp1 = 1;
        int comp2 = 1;

        while ((line = reader.ReadLine()) != null && (line2 = read.ReadLine()) != null)
        {           
            string[] split = line.Split(Convert.ToChar(seperator));
            string[] split2 = line2.Split(Convert.ToChar(seperator));

            if (line.Contains(seperator) && line2.Contains(seperator))
            {
                if (split[comp1] != split2[comp2])
                {
                    //It is not the same
                }
                else
                {
                    //It is the same

                }
            }
        }
        reader.Dispose();
        read.Dispose();
    }
    catch
    {

    }

Here is another way to find differences between CSV files, using Cinchoo ETL - an open source library

For the below sample CSV files

sample1.csv

id,name
1,Tom
2,Mark
3,Angie

sample2.csv

id,name
1,Tom
2,Mark
4,Lu

Using Cinchoo ETL, below code shows how to find differences between rows by all columns

var input1 = new ChoCSVReader("sample1.csv").WithFirstLineHeader();
var input2 = new ChoCSVReader("sample2.csv").WithFirstLineHeader();

using (var output = new ChoCSVWriter("sampleDiff.csv").WithFirstLineHeader())
{
    output.Write(input1.OfType<ChoDynamicObject>().Except(input2.OfType<ChoDynamicObject>(), ChoDynamicObjectEqualityComparer.Default));
    output.Write(input2.OfType<ChoDynamicObject>().Except(input1.OfType<ChoDynamicObject>(), ChoDynamicObjectEqualityComparer.Default));
}

sampleDiff.csv

id,name
3,Angie
4,Lu

If you want to do the differences by 'id' column,

var input1 = new ChoCSVReader("sample1.csv").WithFirstLineHeader();
var input2 = new ChoCSVReader("sample2.csv").WithFirstLineHeader();

using (var output = new ChoCSVWriter("sampleDiff.csv").WithFirstLineHeader())
{
    output.Write(input1.OfType<ChoDynamicObject>().Except(input2.OfType<ChoDynamicObject>(), new ChoDynamicObjectEqualityComparer(new string[] { "id" })));
    output.Write(input2.OfType<ChoDynamicObject>().Except(input1.OfType<ChoDynamicObject>(), new ChoDynamicObjectEqualityComparer(new string[] { "id" })));
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM