Menu
![](/uploads/1/2/5/8/125832278/424644240.jpg)
A simple GUI for python's difflib to compare files and directories - yebrahim/pydiff. Compare two files report difference in python. Ask Question Asked 6 years ago. Is there a better method to compare 2 files and report the difference? Python file comparison. For example in one of my files i have 'Cats' in the other 'Cast'. When I run the code it does exactly what I want except the code prints 'Cats Cast'.
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('somefile1.txt', 'r')file2 = open('somefile2.txt', 'r')FO = open('someoutputfile.txt', 'w')for line1 in file1:for line2 in file2:if line1 line2:FO.write('%sn'%(line1))FO.closefile1.closefile2.closeHowever, 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 = TUVALUBT:TSBallUpdateThreshold = 0.2BT:TSPlayerSearchRadius = 4BT:BallTemplateUpdate = 0while second file (file2) contains data: PoleID = 2Width = 1280Height = 1024ColorMode = 0SensorScale = 1TrackingROISize = 4BallTemplateUpdate = 0If 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.
![Python File Compare Example Python File Compare Example](/uploads/1/2/5/8/125832278/720770402.jpg)
Should I use regex for this problem? I do not have experience with regex. Yet another example. From future import printfunction #Only for Python2with open('file1.txt') as f1, open('file2.txt') as f2, open('outfile.txt', 'w') as outfile:for line1, line2 in zip(f1, f2):if line1 line2:print(line1, end=', file=outfile)And if you want to eliminate common blank lines, just change the if statement to:if line1.strip and line1 line2.strip removes all leading and trailing whitespace, so if that's all that's on a line, it will become an empty string ', which is considered false. If order is preserved between files you might also prefer difflib. Although Robᵩ's result is the bona-fide standard for intersections you might actually be looking for a rough diff-like: from difflib import Differwith open('cfg1.txt') as f1, open('cfg2.txt') as f2:differ = Differfor line in differ.compare(f1.readlines, f2.readlines):if line.startswith(' '):print(line2:, end=')That said, this has a different behaviour to what you asked for (order is important) even though in this instance the same output is produced. Try this: from future import withstatementfilename1 = 'G:test1.TXT'filename2 = 'G:test2.TXT'with open(filename1) as f1:with open(filename2) as f2:file1list = f1.read.splitlinesfile2list = f2.read.splitlineslist1length = len(file1list)list2length = len(file2list)if list1length list2length:for index in range(len(file1list)):if file1listindex file2listindex:print file1listindex + ' + file2listindexelse:print file1listindex + '!=' + file2listindex+' Not-Equel'else:print 'difference inthe size of the file and number of lines'.
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier.The most important thing in Data Analysis is comparing values and selecting data accordingly. The “” operator works for multiple values in a Pandas Data frame too. Following two examples will show how to compare and select data from a Pandas Data frame.To download the CSV file used, ClickExample #1: Comparing DataIn the following example, a data frame is made from a csv file. In the Gender Column, there are only 3 types of values (“Male”, “Female” or NaN). Every row of Gender column is compared to “Male” and a boolean series is returned after that.
![](/uploads/1/2/5/8/125832278/424644240.jpg)