1

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) 
5
  • 1
    , but I have an error. ... What error? Stacktrace? Apr 20, 2021 at 15:08
  • When I use: text_file.write(ln2, '') the error is: TypeError: write() takes exactly one argument (2 given) and when I use: ln2 = '' the error is: IndexError: string index out of range.
    – Reza
    Apr 20, 2021 at 15:13
  • If I should not give two arguments for the write method, then how can I delete that line?
    – Reza
    Apr 20, 2021 at 15:14
  • You cant. You need to a) open 'a.txt' with "w" - wich will truncate it and then write all the data that should end up in it or b) write all the data you want into a new file, delete the old one and rename the new one. Apr 20, 2021 at 15:22
  • Could you add the output you expect?
    – Matthias
    Apr 20, 2021 at 15:27

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.