Wednesday, November 20, 2024
Google search engine
HomeLanguagesPython Program to Find Unique Lines From Two Text Files

Python Program to Find Unique Lines From Two Text Files

In this article, we will discuss finding unique lines from two text files and storing them using Python. To understand the concept we will take the two text file and read the file and find the unique line based on conditions and append it to another file. So, let’s understand the implementation of the code as below:

The first text file:

 

The second text file:

 

The unique lines between them are:

DSA Self-paced

System Design Live

Stepwise Implementation

Step 1: In the first step we will open both text files in “read ” mode using the Python open() function and read all the lines of the file1 and file2(gfg1.txt, gfg2.txt) using the Python readlines() function and the corresponding data is stored in a variable ‘a’ and ‘b’.

a = open('gfg1.txt', 'r').readlines()
b = open('gfg2.txt', 'r').readlines()

Step 2: Another variable ‘output’ is created to store the unique lines between both files.

output = []

Step 3: Now, the second text file is traversed using a for loop, and for every input, a condition is checked whether the given sentence is present in the first text file or not. If the condition is true then the sentence is appended to the list.

for item in b:
    if item not in a:
        output.append(item)

Step 4: A new file says ‘resultant’ is opened in write mode. The list is traversed using for loop and all the corresponding lines are written in the ‘resultant’ file using the Python write() function.

with open('resultant.txt', 'w') as res:
    for line in output:
        res.write(line)

Step 5: Now, we can open the resultant file in our local directory to see the unique line among both files.

Code Implementation

Python3




a = open('gfg1.txt', 'r').readlines()
b = open('gfg2.txt', 'r').readlines()
output = []
 
for item in b:
    if item not in a:
        output.append(item)
 
with open('resultant.txt', 'w') as res:
    for line in output:
        res.write(line)


Output:

 

Method-2:

One alternative approach you could use to find the unique lines between two text files is to use the set data type. A set is an unordered collection of unique elements in Python, which means that it does not allow duplicate elements and does not preserve the order of the elements.

You can use a set to store the lines from one of the text files, and then use the difference method to find the lines that are not present in the other text file. Here is an example of how you could use this approach:

Python3




# Open the first text file in write mode
with open('gfg1.txt', 'w') as file1:
    # Write the additional content to the first text file
    file1.write("neveropen\nSandeep Jain Sir\n")
 
# Close the first text file
file1.close()
 
# Open the second text file in write mode
with open('gfg2.txt', 'w') as file2:
    # Write the additional content to the second text file
    file2.write("neveropen\nSandeep Jain Sir\nDSA Self-paced\nSystem Design Live     ")
 
# Close the second text file
file2.close()
 
# Open the first text file in read mode
with open('gfg1.txt', 'r') as file1:
    # Read the lines from the first text file
    lines1 = file1.readlines()
 
# Convert the lines from the first text file to a set
lines1_set = set(lines1)
 
# Open the second text file in read mode
with open('gfg2.txt', 'r') as file2:
    # Read the lines from the second text file
    lines2 = file2.readlines()
 
# Convert the lines from the second text file to a set
lines2_set = set(lines2)
 
# Find the unique lines between the two text files
unique_lines = lines2_set.difference(lines1_set)
 
# Iterate over the unique lines and print them
for line in unique_lines:
    print(line)


This will find the unique lines between the two text files .

Output:

DSA Self-paced

System Design Live  

Steps:

  1. Open the first text file in “write” mode using the open function.
  2. Write the content to the first text file using the write method.
  3. Close the first text file using the close method.
  4. Open the second text file in “write” mode using the open function.
  5. Write the additional content to the second text file using the write method.
  6. Close the second text file using the close method.
  7. Open the first text file in “read” mode using the open function.
  8. Read the lines from the first text file using the readlines method.
  9. Convert the lines from the first text file to a set data type.
  10. Open the second text file in “read” mode using the open function.
  11. Read the lines from the second text file using the readlines method.
  12. Convert the lines from the second text file to a set data type.
  13. Find the unique lines between the two text files using the difference method on the sets.
  14. Iterate over the unique lines using a for loop, and print each line using the print function.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments