简体   繁体   English

比较C#中的两个csv文件

[英]Compare two csv files in C#

I want to compare two csv files and print the differences in a file. 我想比较两个csv文件并在文件中打印差异。 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? 我可以更改此代码,以便比较两个csv文件吗?还是在c#中有更好的方法比较csv文件?

  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 这是使用Cinchoo ETL (开源库)查找CSV文件之间差异的另一种方法

For the below sample CSV files 对于以下示例CSV文件

sample1.csv sample1.csv

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

sample2.csv 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 下面的代码使用Cinchoo ETL显示如何按所有列查找行之间的差异

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 sampleDiff.csv

id,name
3,Angie
4,Lu

If you want to do the differences by 'id' column, 如果您想按“ id”列进行区别,

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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