def remove_common_ordered(text1, text2): lines1 = text1.splitlines() lines2 = text2.splitlines() # 构建文本2的行集合用于快速查找 set2 = set(lines2) # 保留text1中不存在于text2的行 result_lines = [line + '\n' for line in lines1 if line not in set2] return ''.join(result_lines) if __name__ == '__main__': # 0到465 for i in range(2, 467): md1 = "./md/1.md" mdn = "./md/" + str(i) + ".md" mdnn = "./mdd/" + str(i) + ".md" text = '' with open(md1, "r", encoding="utf-8") as f: markdown_text = f.read() text += markdown_text text1 = '' with open(mdn, "r", encoding="utf-8") as f: markdown_text = f.read() text1 += markdown_text output = remove_common_ordered(text1, text) with open(mdnn, "w", encoding="utf-8") as f: f.write(output) # 结果写入新文件