The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
how to store Diff
Discuss how to store Diff in the Python Programming forum on Dev Shed. how to store Diff Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 7th, 2004, 06:05 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
how to store Diff
Is tehre a way i can compare and store the diff to third file
Code:
import os
file1=('num1.log')
file2=('num2.log')
file3=('num3.log')
for i in file(file1):
i=i.split()
print (' '.join(i)),
print "\n"
for j in file(file2):
j=j.split()
print(' '.join(j)),
|

July 7th, 2004, 07:06 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Do you require it in a specific format?
You can call an external program like diff:
Code:
import os
os.system("diff %s %s > %s"%(file1,file2,file3))
BTW you don't need ( ) around the print statements and filename assignements.
grim 
Last edited by Grim Archon : July 7th, 2004 at 07:10 AM.
|

July 7th, 2004, 07:19 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
|
You could also try the difflib module. See the Python docs for an example.
|

July 7th, 2004, 07:35 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
I tried your code it creates an empty file called file3.Doesn't it copy the diffs from file1 and file2 to file3
Thanks
|

July 7th, 2004, 08:27 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
If file1 contents are the same as file2 contents then the output will be nothing (also true if they happen to be the same file).
I'm gusssing that you have diff on your platform.
grim 
|

July 7th, 2004, 08:51 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
contents in file1 and file2 are not exactly same
contents of file1=1, 2, 3, 4, 5, 6, 56,
contents of file2=2, 6, 12, 58, 96, 56,
contents of file3 should be =2, 6, 56
Thanks
|

July 7th, 2004, 09:19 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
Quote: | Originally Posted by pyton contents in file1 and file2 are not exactly same
contents of file1=1, 2, 3, 4, 5, 6, 56,
contents of file2=2, 6, 12, 58, 96, 56,
contents of file3 should be =2, 6, 56
Thanks |
What you want is not the file difference but the file intersection.
For general file comparison you can use difflib or an external diff program, as grim said. However these are mainly for doing context diffs, which show a line by line difference - for your example data it would display something like
Code:
1- 1, 2, 3, 4, 5, 6, 56,
1+ 2, 6, 12, 58, 96, 56,
this shows that a line has been removed and a new one added, which is not what you want.
From your example I think what you really want to do is not show the difference, but to remove it altogether and show the common data. This is not a normal use for diff, although difflib could be used to do this with some careful coding.
If the data files are always going to be comma separated lists of values then one possibility would be to read the values into sets and use set the intersection method to find common values. This is assuming that the order of the values is not important, since sets are unordered.
Some questions to think about :
1) are the files in CSV format? If so then you can use the csv module to read them in.
2) do you want to compare the files line by line, i.e. only comparing line 1 of file 1 with line 1 of file 2?
3) is the order of the entries important? What about duplicate values? If neither of these are important then you can use sets.
4) what is the context of the problem? What is the higher level problem that this is trying to solve? There may be other ways of solving the higher-level problem than doing a diff between files.
Dave - The Developers' Coach
|

July 7th, 2004, 09:33 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Yes I got the point I want to compare the files line by line
Thanks
|

July 7th, 2004, 10:26 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
Hi
Thanks for the help and advice
Here is something i tried but it displays all the contents of file1 and file2 and also it put's "+" and "- " sign i don't know what it means
Haven't tried to store it to file3 as the result is not what i want (want the differences)
Code:
import os
file1=('num1.log')
file2=('num2.log')
file3=('num3.log')
first=file(file1).readlines()
secon=file(file2).readlines()
diff=difflib.ndiff(first,secon)
for line in diff:
line=line.strip()
#total=len(line)
print line.rstrip()#total
Thanks for any help
|

July 8th, 2004, 02:25 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
would appriciate for some help for the previous query
Thanks
|

July 8th, 2004, 03:21 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
I think DevCoach explained this for you
The difflib produces an output that programmatically describes the differences between the files (this is actually what I thought you required by the description in your original post). However, you say you actually just want an output file that just contains by line what is in file2 but not in file1 .
If you answer DevCoach's questions 1,3 amd 4 we may be able to help further.
It would help if you posted a real example of file1 and file 2 where the difference between them is clear. (2 or 3 lines from each file is enough).
BTW
file1=('num1.log')
file2=('num2.log')
file3=('num3.log')
would normally be ...
file1='num1.log'
file2='num2.log'
file3='num3.log'
grim 
|

July 8th, 2004, 03:58 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
I want check line by line
Here are the line for file1 and file2
file1
Consistency checks are internal tests which software engineers have placed system code. The primary function of consistency checks is to ensure the stability and integrity of internal operating system data.
file2
Consistency checks are internal tests which software engineers have placed system code. The primary function of consistency checks is to ensure the stability and integrity of internal operating system data. Numerous consistency checks are interlaced throughout
the answer in file3 should be the line
Numerous consistency checks are interlaced throughout
|

July 8th, 2004, 04:57 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Based on the info you have provided here is a basic solution I am sure you can extend to do what you want:
Code:
file1 = 'num1.log'
file2 = 'num2.log'
file3 = 'num3.log'
first = file(file1).readlines()
secon = file(file2).readlines()
for n in range(len(first)):
lenf = len(first[n].strip())
diff = secon[n][lenf: ].strip()
print "Line %s: ", diff
Last edited by Grim Archon : July 8th, 2004 at 04:59 AM.
|

July 8th, 2004, 07:24 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Thanks for all the help I shall modify the code according to my needs once again
Thanks
|

July 8th, 2004, 08:33 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
You could always use this generator to loop over one or more objects at the same time.
Code:
def group(*objects):
while True:
results = []
for object in objects:
results.append(object.next())
yield tuple(results)
for a, b in group(file('file1.txt'), file('file2.txt')):
if a != b: print b,
This has is limitations, since it will only iterate to the end of the smallest file but this could easily be extended if need be. But the example does show how easy it is to compare two lines in a file.
Have fun,
Mark.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : July 8th, 2004 at 08:59 AM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|