I’m new to Python programming so forgive me if my code is not efficient, etc. I need to compare text file B with text file B and print the results out to another file. Simply put, result C = text file A – text file B.
I have the following code which works, however, there are duplicate results due to upper and lower cases. How can I make it such that my program can compare it without case sensitivity?
#!/usr/local/bin/python -u
file1='A_GAGL.txt'
file2='B_GGL.txt'
def key(line):
return tuple(line.strip().split()[0:2])
def make_key_set(file_path):
return set(key(line) for line in open(file_path))
def filtered_lines(file_path1, file_path2):
key_set = make_key_set(file_path2)
return (line for line in open(file_path1) if key(line) not in key_set)
if __name__ == "__main__":
file3 = open("file4.txt", "w")
for line in filtered_lines(file1, file2):
file3.write(line)
file3.close()
Many thanks in advance
In
make_key_set, convert everything to lowercase:Then in
filtered_linescheck whether the lowercase line is inkey_set(but return the original-case line):