我有一个csv文件,其中有2列,即``日期''和``文件名''。我在某些目录的``文件名''列中提到了这些文件。我想检查csv中提到的“日期”是否与csv中针对它提及的特定“文件名”的内容中的任何日期匹配,如果不是,那么我想获取包含所有内容的csv文件中的输出日期中不匹配的文件名。
csv示例:
Date filenames
03-02-2017 abc
02-09-2007 xyz
代码:-
DIR = 'C:\\Users\\xyzk\\Global_TxtFiles\\'
with open(r'C:\Users\xyzk\form eng recvd date1.csv', newline='',encoding='latin-1') as myFile:
reader = csv.reader(myFile)
for row in reader:
try:
date = row[0]
filename = row[1]
file = open(DIR+filename+'.txt',"r", encoding ='utf-8')
content=file.read()
matches = datefinder.find_dates(content)
datefinder.ValueError = ValueError, OverflowError
fd=[]
for match in matches:
fd.append(match)
date_strings3=[d.strftime('%d-%m-%Y') for d in fd]
header=['filename']
with open(r'C:\Users\xyzk\filenames.csv',mode='w') as csvFile:
writer = csv.writer(csvFile,header)
for x in date_strings3:
if x!=date:
row=[filename]
writer.writerow(row)
except Exception as e:
print(e)
pass
但是csv编写器仅输出最后一个'filename':/