csv - 如何通过python比较2个CSV文件的特定列
问题描述
Python 版本 - 2.6.6
1.csv
10.10.1.1,Web server,test,http,Running
10.10.1.1,Web server,test,http1,Running
2.csv
10.10.1.1,Web server,test,http,Not Running
10.10.1.1,Web server,test,http1,Not Running
我尝试使用下面的代码来比较特定列的 2 个 csv 文件,但它不起作用。我需要根据实际结果输出,请在下面指导
import csv
s=open('1.csv')
checkit = csv.reader(s)
for c in checkit:
o=open('2.csv')
csv_o = csv.reader(o)
for row in csv_o:
if (row[4] == c[4]) and (row[0] == c[0]):
print 'equal =',row[0],',',row[3],',',row[4]
else:
print 'Differenece =',row[0],',',row[3],',',row[4]
实际结果是
Difference = 10.10.1.1, http,Not Running
Difference = 10.10.1.1, http1,Not Running
Difference = 10.10.1.1, http,Not Running
Difference = 10.10.1.1, http1,Not Running
预期的结果是
Difference = 10.10.1.1, http,Not Running
Difference = 10.10.1.1, http1,Not Running
需要在不同的 csv 中写入预期结果。请帮忙
解决方案
推荐阅读
- java - Javafx 场景构建器 CSS SetStyle 覆盖
- python - 用 pandas 将硬编码字符串换成变量时出现标量错误
- python - 如何使用 Selenium 和 Python 启动 Tor 浏览器
- javascript - ThreeJs 从另一个对象创建对象
- java - LogRecord 的方法解析
- c++ - 在 C++ 中使用 RapidJSON 访问 JSON 数组的字段
- php - php: if (file_exists($file)) 对某些文件返回 true,对其他文件返回 false
- kubernetes - helm rollback 在 Kubernetes 中是如何工作的?
- powershell - Powershell DSC,将 MOF 输出到不同的文件夹
- django-viewflow - Viewflow Process 不会通过 split-n-join 任务干净地退出?