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
          

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM