利用python实现文件的筛选与对比
import csv
import os
Student_CouncilMember = open(input("请输入需要筛选的文件路径:"), "r")
Student_All = open(input("请输入筛选文件的路径:"), "r")
pa = input("请输入输出的路径及文件名:")
Student_ListOutput = open(pa, "w")
def FileInput(filename):
ls = []
lines = filename.readlines()
for line in lines:
ls.append(line.strip().split("\n"))
return ls
list_CouncilMember = FileInput(filename=Student_CouncilMember)
list_StudentAll = FileInput(filename=Student_All)
def contrast():
i = int(input("请输入被筛选姓名所在列数:")) - 1
j = int(input("请输入筛选文件中姓名所在列数:")) - 1
li = []
lt = []
for councilman in list_CouncilMember:
for z in councilman:
li.append(z.split(","))
for student in list_StudentAll:
for h in student:
lt.append(h.split(","))
list_Council = []
for x in li:
for y in range(len(lt)):
if x[i] in lt[y][j]:
list_Council.append(lt[y])
else:
continue
writer = csv.writer(Student_ListOutput)
writer.writerows(list_Council)
contrast()
print("运行结束,文件路径:", os.path.realpath(pa))
Student_CouncilMember.close()
Student_All.close()
Student_ListOutput.close()
|