下面是一个示例代码,用于比较两个平面文件的记录,并将FILE-1中不匹配的记录写入输出。
def compare_records(file1, file2, output):
with open(file1, 'r') as f1, open(file2, 'r') as f2, open(output, 'w') as out:
# 读取文件1的记录
records1 = f1.readlines()
# 读取文件2的记录
records2 = f2.readlines()
# 比较记录并将不匹配的记录写入输出
for record in records1:
if record not in records2:
out.write(record)
在调用compare_records函数时,需要传入三个参数:file1、file2和output,分别是要比较的两个文件和输出文件的路径。
例如,假设文件1的路径为file1.txt,文件2的路径为file2.txt,输出文件的路径为output.txt,可以这样调用函数:
compare_records('file1.txt', 'file2.txt', 'output.txt')
这样,函数将会比较file1.txt和file2.txt中的记录,并将不匹配的记录写入output.txt。