Python 3.6 list对比并输出差异

y t  2列数据对比,输出 T中的新增元素存放到txt文件中



# -*- coding:utf-8 -*-
import os

yesterdaynamelist=[]
todaynamelist=[]
differentnamelist=[]
areceivername=[]
test=[]
#读取 昨天生成的namelist 文件 并生成todaynamelist
namelist = open('D:\\python\\Project\\AtuoEmail\\Date\\riqi.txt','r')
linea = namelist.readlines()
# lineb = namelist.readline()
# print (namelist)
# print (linea)
# print (lineb)
# for i in linea:
#     print (i)
#
for i in linea:
    line=i.split()
    # print(line)
    yesterdaynamelist.extend(line)
    # print(yestdaynamelist)


# todaynamelist 列表输出成单列的文本。
yesterdaytxt = open("D:\\python\\Project\\AtuoEmail\\Date\\yesterdaytxt.txt","w",encoding="utf-8")  #w参数 创建+复写
yesterdaytxt.close()
for i in yesterdaynamelist:
     # print (i)
     # print(type(i))
     yesterdaytxt = open("D:\\python\\Project\\AtuoEmail\\Date\\yesterdaytxt.txt","a",encoding="utf-8")
     yesterdaytxt.write(i)
     yesterdaytxt.write("\n")
yesterdaytxt.close()
# todaytxt.write(todaynamelist)
# todaytxt.close()  #关闭文件

# Yesterdaytxt
# temp

# print (line)
# for i in line:
#     print i.strip().split()[0]
#     print i.strip().split()[1]
#     print i.strip().split()[2]


#调用 cmd生成当天最新的域控用户名单。
os.system('D:\\python\\Project\\AtuoEmail\\TodayADUser.bat')

namelist = open('D:\\python\\Project\\AtuoEmail\\Date\\riqi.txt','r')
linea = namelist.readlines()
todaynamelist=[]
for i in linea:
    line=i.split()
    # print(line)
    todaynamelist.extend(line)
    # print(todaynamelist)

# print(todaynamelist)
# print(yesterdaynamelist)
# print(list(set(todaynamelist).difference(set(yesterdaynamelist))))

#生成差异名单并导出文件
differentnamelist=list(set(todaynamelist).difference(set(yesterdaynamelist))) #“t”“y”对比,输出“T”中新增的元素
# print(differentnamelist)
for i in differentnamelist:
#      print (i)
#      print(type(i))
      differentnamelist = open("D:\\python\\Project\\AtuoEmail\\Date\\differentnamelist.txt","a",encoding="utf-8")
      differentnamelist.write(i)
      differentnamelist.write("@dafy.com,")
differentnamelist.close()

你可能感兴趣的:(Python 3.6 list对比并输出差异)