如何在 Windows 中比较两个 csv 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33523362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two csv files in windows
提问by XYZ_Linux
I need to compare two csv files in windows7. How can I proceed to achieve this. I want to see the differences in both the files , like we can use tkdiff in Linux.
我需要比较 windows7 中的两个 csv 文件。我该如何继续实现这一目标。我想看看这两个文件的不同之处,就像我们可以在 Linux 中使用 tkdiff 一样。
回答by user5524993
Suggestion:
建议:
- Press Windows+Rshortcut to open windows' Run prompt
- Type in
cmd
and press Enterto open aDOS terminalcmd window - Change the current path by running the command
cd C:\path\to\your\directory
to reach the location of the two CSV files
- 按Windows+R快捷键打开 Windows 的运行提示
- 输入
cmd
并按下Enter以打开DOS 终端cmd 窗口 - 通过运行命令更改当前路径
cd C:\path\to\your\directory
以到达两个 CSV 文件的位置
Tip: To paste a copied path from clipboard into DOS terminalcmd window, you can either (1) right-click on terminal window, or (2) press Shift+Insert.
提示:要将剪贴板中复制的路径粘贴到DOS 终端cmd 窗口,您可以 (1) 右键单击终端窗口,或 (2) 按Shift+ Insert。
- Finally, to compare the two files, run
fc filename1.csv filename2.csv > outfile.txt
(fc
stands for "file compare").
The command will also log the result of comparison into a text fileoutfile.txt
located in the same folder. Ifoutfile.txt
doesn't exist, it will be created automatically.
- 最后,要比较两个文件,请运行
fc filename1.csv filename2.csv > outfile.txt
(fc
代表“文件比较”)。
该命令还将比较结果记录到outfile.txt
位于同一文件夹中的文本文件中。如果outfile.txt
不存在,它将自动创建。
回答by S3DEV
Here is another option which I found very useful, as mentioned here:
下面是我发现非常有用,因为提到的另一种选择在这里:
findstr /v /g:"file1.csv" "file2.csv"
findstr /v /g:"file1.csv" "file2.csv"
Where the /v
switch returns the differences and /g:
gets the search strings from file1.csv. You can use findstr /?
for more help.
当/v
开关返回的差异,并/g:
从file1.csv得到搜索字符串。您可以使用findstr /?
以获得更多帮助。
You can also print the differences to a fileusing:
您还可以使用以下方法将差异打印到文件中:
findstr /v /g:"file1.csv" "file2.csv > diffs.csv"
findstr /v /g:"file1.csv" "file2.csv > diffs.csv"
As an aside, I found findstr
far more accurate and the output more readable than fc
.
顺便说一句,我发现findstr
比fc
.
UPDATE
This works nicely with 'smaller' files. You might get an out of memory
error on larger files. In this case, I've had to turn to Python and dataframes. Just a friendly heads up ...
更新
这适用于“较小”的文件。out of memory
对于较大的文件,您可能会收到错误消息。在这种情况下,我不得不求助于 Python 和数据帧。只是一个友好的抬头......
回答by rohan_shedge
I did this today.
我今天做了这个。
Lets say we have 2 csv files X and Y
假设我们有 2 个 csv 文件 X 和 Y
X having columns a, b, c
Y having column a, b, c
X 有 a、b、c
列 Y 有 a、b、c 列
The rows are not in same order and are disperesed througout the csv files.
行的顺序不同,并且在 csv 文件中分散。
I imported both of them in my excel sheet. I sorted them first by column c and then by column b and then by column a. You can go in any order you like.
我在我的excel表中导入了它们。我先按 c 列排序,然后按 b 列排序,然后按 a 列排序。你可以按你喜欢的任何顺序去。
Compare the sorted files through notepad++'s compare plugin/Beyond Compare.
通过记事本++的比较插件/Beyond Compare比较排序后的文件。