{"id":26727,"date":"2022-04-01T00:19:55","date_gmt":"2022-03-31T18:49:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=26727"},"modified":"2022-04-01T00:19:55","modified_gmt":"2022-03-31T18:49:55","slug":"how-to-find-unique-lines-from-two-text-txt-files-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-find-unique-lines-from-two-text-txt-files-in-python\/","title":{"rendered":"How to Find Unique Lines from two Text(.txt) Files in Python?"},"content":{"rendered":"
In this article, let us see\u00a0how to use Python to find only the unique lines in two text\u00a0files (.txt\u00a0files). We may also state that we will use Python to remove the lines that already exist in both of the text (.txt) files and place the new lines in another.txt file.<\/p>\n
Let us consider the below two text files as an example:<\/p>\n
demotextfile_1.txt:<\/strong><\/p>\n demotextfile_2.txt:<\/strong><\/p>\n The output of our code must be as follows:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:(output_file.txt)<\/strong><\/p>\n NOTE:<\/strong> If the file output_file.txt<\/strong>\u00a0already exists, it will simply be updated.<\/p>\n","protected":false},"excerpt":{"rendered":" In this article, let us see\u00a0how to use Python to find only the unique lines in two text\u00a0files (.txt\u00a0files). We may also state that we will use Python to remove the lines that already exist in both of the text (.txt) files and place the new lines in another.txt file. Let us consider the below …<\/p>\nhello this is Python-programs\r\ngood morning all<\/pre>\n
hello this is Python-programs\r\nAre you good at coding? \r\nThen give a start to it\r\nwelcome to the greatest python coding platform\r\ngood morning all<\/pre>\n
Are you good at coding? \r\nThen give a start to it\r\nwelcome to the greatest python coding platform<\/pre>\n
Finding Unique Lines from two Text(.txt) Files in Python<\/h2>\n
\n
# Open some random text file in read-only mode using the open() function \r\n# and read all the lines of the file using the readlines() function.\r\n# Store it in a variable.\r\ngvn_txtfile_1= open('demotextfile_1.txt','r').readlines()\r\n# Similarly, Open the second text file in read-only mode using the open() function \r\n# and read all the lines of the file using the readlines() function.\r\n# Store it in another variable.\r\ngvn_txtfile_2 = open('demotextfile_2.txt','r').readlines()\r\n# Take a new empty list and store it in another variable.\r\nlines_lst= []\r\n# Loop in the given second text file using the for loop\r\nfor line in gvn_txtfile_2:\r\n # Check if each line of the second file is NOT present in the given first text \r\n # file using the if conditional statement.\r\n if line not in gvn_txtfile_1:\r\n # If it is true(unique), then append that line to the above created new list\r\n lines_lst.append(line)\r\n\r\n# Open some random new file in write mode using the open() function \r\nwith open('output_file.txt','w') as outfile:\r\n # Loop the above created new list(which contains unique lines) using the for loop\r\n for line in lines_lst:\r\n # Write the corresponding line to the given outputfile using the write() function\r\n outfile.write(line)<\/pre>\n
Are you good at coding? \r\nThen give a start to it\r\nwelcome to the greatest python coding platform<\/pre>\n