我正在尝试制作一个由 3 部分组成的程序。我有两个文本文件,文本文件 A 和文本文件 B
文本文件A必须将数据与文本文件B进行比较。如果有重复,则会通过电子邮件发送。 如果没有重复,则将数据写入文本文件B。
我遇到的问题是,复制总是从文本文件的开头开始。我正在尝试查找代码,从检查的最后一行开始。
这是我的重复代码。
f1 = open("/path/to/file1", "r")
f2 = open("/path/to/file2", "r")
txtfileA = f1.read()
txtfileB = f2.read()
txtfileA_words = txtfileA.split()
txtfileB_words = txtfileB.split()
result = set(textfileA_words).difference(set(txtfileB_words))
print result
是否有更好的代码集,我做错了什么吗?
任何建议都会很好。
最佳答案
您应该使用交集函数,而不是差异函数。另外,您的变量名称有一个拼写错误。
f1 = open("/path/to/file1", "r")
f2 = open("/path/to/file2", "r")
txtfileA = f1.read()
txtfileB = f2.read()
txtfileA_words = txtfileA.split()
txtfileB_words = txtfileB.split()
# remove the extra e in textfileA_words and use intersection
result = set(txtfileA_words).intersection(set(txtfileB_words))
print result
关于python - 比较两个文本列表,检查是否重复并将其标记为行尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24196610/