比较两个csv文件的特定列 [英] Comparing specific columns of two csv files

查看:181
本文介绍了比较两个csv文件的特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个csv文件,每个像18列...我想用c#比较第一个文件的第一列与第二个文件的第一列和第一列的第三列与第三列第二个,当发现差异时,我想要保存到另一个文件,所以第三个文件应该有一个输出两列。不知何故我需要告诉哪个是第一列,哪个是第三列,然后比较。

I have two csv files with like 18 columns each...I would like with c# to compare the first column of the first file with the first column of the second file and the third column of the first with the third column of the second and when a difference is found,I want to be saved to another file so the third file should have as an output two columns.So somehow I need to tell which is the first column and which is the third column and then compare.

任何建议如何实现这一点?

Any suggestions how can i achieve this?

推荐答案

正在逐行读取每个文件,然后拆分每个字符串。对于CSV文件,通常有一个逗号作为分隔符,但这可能是一个选项卡或类似的字符。所以使用你有一个。然后你会得到类似

The basics are reading each file line by line and then splitting each string. With a CSV file you typically have a comma as seperator, but this could be a tab or similar char as well. So use the one you have. You then get something similar to

        string line;
        System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.csv");
        while ((line = file.ReadLine()) != null)
        {
            var arr = line.Split(new char[] { ',' });
            // do your comparison
        }

。然后,可以将文件1的数组与文件2的数组(index转换为array ==列,从0开始)进行比较。类似地,您可以使用String.Join重新创建输出。

though you nee to open 2 files. You can then compare the array from file 1 with the array from file 2 (index into array == column, starting at 0). Smilarly, you can use String.Join to create your output again.

这篇关于比较两个csv文件的特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆