I have two files (file1 and file2) and want to compare each line of file2 with all lines in file1. And if this condition is satisfied:
(ln2[1][0] != ln1[1][0]) and (ln2[1][0] != '-')
then I want to remove the current line of file2, i.e. ln2, from the file2.
I wrote this code in python, but I have an error.
arr1 = [] with open('FA1.txt', 'r') as file1: Lines1 = file1.readlines() for line in Lines1: arr1.append(line.split()) arr2 = [] with open('a.txt', 'r') as file2: Lines2 = file2.readlines() for line in Lines2: arr2.append(line.split()) for ln2 in arr2: for ln1 in arr1: if (ln2[1][0] != ln1[1][0]) and (ln2[1][0] != '-'): text_file = open("a.txt", "a+") text_file.write(ln2, '') text_file.close() #ln2 = ''
Also, instead of the last three lines of code, I used: (ln2 = '')
, But I had the error: IndexError: string index out of range
.
The content of files is like this:
File1: 010 00 001 11 010 10 File2: 00- 0- 0-0 0- 1-- -0 -10 0-
I get the following error:
TypeError: write() takes exactly one argument (2 given)
Advertisement
Answer
Rewriting the code gives us:
with open('FA1.txt', 'r') as file1: arr1 = [line.split() for line in file1] with open('a.txt', 'r') as file2: arr2 = [line.split() for line in file2] with open("b.txt", "w") as text_file: for line2 in arr2: for line1 in arr1: if (line2[1][0] != line1[1][0]) and (line2[1][0] != '-'): text_file.write(' '.join(line2) + 'n')
I used a different file for the output because I didn’t want to destroy my test data.
But now your resulting file b.txt
will contain duplicate lines because of the loop and you output all lines that match the condition while they should be deleted.
00- 0- 00- 0- 0-0 0- 0-0 0- -10 0- -10 0-
According to your question you want to compare each line of the second file with each line of the first file and write the data if not any one of those match. The code needs to reflect this, so we use if not any ...
.
with open('FA1.txt', 'r') as file1: arr1 = [line.split() for line in file1] with open('a.txt', 'r') as file2: arr2 = [line.split() for line in file2] with open("b2.txt", "w") as text_file: for line2 in arr2: if not any((line2[1][0] != line1[1][0]) and (line2[1][0] != '-') for line1 in arr1): text_file.write(' '.join(line2))
The result is
1-- -0
A short explanation of arr1 = [line.split() for line in file1]
. I used a list comprehension to make the code shorter. And a file is iterable, so I don’t have to use readlines
here.