I have two text files:
First text file:
[email protected]:password
[email protected]:password
[email protected]:password
[email protected]:password
[email protected]:password
2nd text file:
[email protected]:password
[email protected]:password
How can I output the differences between them regardless of the line number? I want to output a third file:
third text file:
[email protected]:password
[email protected]:password
[email protected]:password
CodePudding user response:
Assuming that the username and password are separated by colon then you could do this:
files = ['file1.txt', 'file2.txt']
usernames = [set(), set()]
for i, file in enumerate(files):
with open(file) as infile:
for line in infile:
usernames[i].add(line.split(':')[0])
for d in usernames[0] - usernames[1]:
print(d)
CodePudding user response:
file1Lines = open("file1.txt", "r").readlines()
file2Lines = open("file2.txt", "r").readlines()
file1Lines.close()
file2Lines.close()
file3LinesOutput = []
for line in file1Lines:
if line not in file2Lines:
file3LinesOutput.append(line)
file3 = open("file3.txt", "w")
file3.writelines(file3LinesOutput)
filr3.close()
This should do what you asked for. It will add to a third file every line that's in the first file but not in the second one
CodePudding user response:
You can split this into a list.
Let's assume that the first text is stored in the variable first_text, the second one, second_text.
As the only thing that differenciates one from another is the break line, you could split them with:
first_list = first_text.split('\n')
second_list = second_text.split('\n')
So, now you have all your e-mails separated in lists. From here you can just compare both lists and store in output_text.
output_list = first_list
for email_first in first_list:
for email_second in second_list:
if (email_first == email_second):
output_list.remove(email_first)
Note that this is for your specific example, and doesn't cover properly a case in which the second list is bigger than the first one.