python操作csv,对比两个csv文件某列值
功能:
对比两个csv文件中的某列值,csv文件1中存在,文件2中不存在。
#!/usr/bin/env python #-- encoding:utf-8 -- import csv all_list = [] agent_list = [] def ecsInsAll(): with open('/Documents/egion_2020.csv', 'U') as csvfile: reader = csv.DictReader(csvfile) all_list = [row["private_ip"] for row in reader] return all_list def agentAll(): with open('/Documents/sqlResult.csv', 'U') as csvfile: reader = csv.DictReader(csvfile) agent_list = [row["ipAddress"] for row in reader] return agent_list if __name__ == '__main__': all_list = ecsInsAll() agent_list = agentAll() print "阿里云ECS机器总数:",len(all_list) print "已经安装agent机器总数:",len(agent_list) e = [y for y in all_list if y not in agent_list] #在c_list列表中而不在all_list列表中 # print '没有安装agent的机器IP:',len(e),e print '没有安装agent的机器IP,总数:',len(e) i = 1; for name in e: # print name with open('/Documents/list_all_region_2020.csv', 'U') as csvfile: reader = csv.DictReader(csvfile) for row in reader: if name == row["private_ip"]: print i,row["private_ip"],row["Region"] i=i+1