[英]Check if there are new strings in a txt file
I am trying to make a function which will compare two txt
files. 我正在尝试制作一个将比较两个
txt
文件的函数。 If it recognizes new lines that are in one file but not in the other, it will add them in a list
and also in that file that does not contain those new lines. 如果它识别一个文件中的换行符,但不能识别另一个文件中的换行符,则会将它们添加到
list
,也可以添加到不包含这些新行的文件中。 It fails to do that. 它无法做到这一点。 Here is my function.
这是我的功能。 What am I doing wrong?
我究竟做错了什么?
newLinks = []
def newer():
with open('cnbcNewLinks.txt', 'w') as newL:
for line in open('cnbcCleanedLinks.txt'):
if line not in "cnbcNewLinks.txt":
newLinks.append(line)
newL.write(line)
else:
continue
cleaned = ''.join(newLinks)
print(cleaned)
If files not big, then move data in list, both of list convert in set and use 'differ' builtin functions, two times. 如果文件不大,则将数据移动到列表中,列表中的两个都将转换为set并使用两次“ differ”内置函数。 then add difference in files.
然后在文件中添加差异。
I put in python code what @Alex suggested. 我输入了@Alex建议的python代码。
See the doc for set
. 请参阅文档
set
。
I replace you text file name by a.txt
and b.txt
to be easily readable. 我将文本文件名替换为
a.txt
和b.txt
以便于阅读。
# First read the files and compare then using `set`
with open('a.txt', 'r') as newL, open('b.txt', 'r') as cleanL:
a = set(newL)
b = set(cleanL)
add_to_cleanL = list(a - b) # list with line in newL that are not in cleanL
add_to_newL = list(b - a) # list with line in cleanL that are not in newL
# Then open in append mode to add at the end of the file
with open('a.txt', 'a') as newL, open('b.txt', 'a') as cleanL:
newL.write(''.join(add_to_newL)) # append the list at the end of newL
cleanL.write(''.join(add_to_cleanL)) # append the list at the end of cleanL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.