Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two different files line by line in python

Tags:

python

file

I have two different files and I want to compare theirs contents line by line, and write their common contents in a different file. Note that both of them contain some blank spaces. Here is my pseudo code:

file1 = open('some_file_1.txt', 'r') file2 = open('some_file_2.txt', 'r') FO = open('some_output_file.txt', 'w')  for line1 in file1:     for line2 in file2:         if line1 == line2:             FO.write("%s\n" %(line1))  FO.close() file1.close() file2.close() 

However, by doing this, I got lots of blank spaces in my FO file. Seems like common blank spaces are also written. I want to write only the text part. Can somebody please help me.

For example: my first file (file1) contains data:

Config: Hostname = TUVALU  BT: TS_Ball_Update_Threshold = 0.2  BT: TS_Player_Search_Radius = 4  BT: Ball_Template_Update = 0 

while second file (file2) contains data:

Pole_ID      = 2 Width        = 1280 Height       = 1024 Color_Mode   = 0 Sensor_Scale = 1  Tracking_ROI_Size = 4 Ball_Template_Update = 0 

If you notice, last two lines of each files are the same, hence, I want to write this file in my FO file. But, the problem with my approach is that, it writes the common blank space also. Should I use regex for this problem? I do not have experience with regex.

like image 501
Sanchit Avatar asked Sep 25 '13 14:09

Sanchit


People also ask

How do you compare two files in Python?

Specifically, this module is used to compare data between two or more files. We can do this using the filecmp. cmp() method. This method will return True if the files match, or False if they don't.

How can you compare the contents of two different files?

Use the diff command to compare text files. It can compare single files or the contents of directories. When the diff command is run on regular files, and when it compares text files in different directories, the diff command tells which lines must be changed in the files so that they match.

What is == comparing in Python?

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None .


1 Answers

This solution reads both files in one pass, excludes blank lines, and prints common lines regardless of their position in the file:

with open('some_file_1.txt', 'r') as file1:     with open('some_file_2.txt', 'r') as file2:         same = set(file1).intersection(file2)  same.discard('\n')  with open('some_output_file.txt', 'w') as file_out:     for line in same:         file_out.write(line) 
like image 66
Robᵩ Avatar answered Oct 04 '22 03:10

Robᵩ