Python 3 6 list對比並輸出差異

2021-08-04 08:22:11 字數 2948 閱讀 5270

y t  2列資料對比,輸出 t中的新增元素存放到txt檔案中

# -*- coding:utf-8 -*-

importos

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)

#foriinlinea:

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()

foriinyesterdaynamelist:

# 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=

foriinlinea:

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)

foriindifferentnamelist:

# 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()

Python3 6 List內建方法

list 的內建方法 li 1,2 3,4 print li li 1 list 1,2 3,4 接收乙個iterable,可迭代即可 print li 0 可以使用下標訪問元素 print li print li 1,2,3,4,aabb print li 1,2,3,4,aabb 1234 de...

Python中兩個list元素對比

set是乙個無序不重複元素集,python資料型別的一種,由於是無序的,不能通過索引和切片來做一些操作。主要有新增 刪除 交集 並集 差集 對稱差集 五種操作。a set 1,2,3 方法1 新增1項 a.add 4 方法2 新增多項,update中的引數必須是迭代器 a.update 4,5,6 ...

linux命令(36) vimdiff檔案對比

首先保證系統中的diff命令是可用的。vim的diff模式是依賴於diff命令的。vimdiff的基本用法就是 vimdiff file left file right 或者 vim d file left file right除了用這種方法啟動vim的diff模式之外,我們還可以用分割視窗命令來啟...