473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

File compare

I have two files
file1 in format
<id> <val1> <test1> <test2>
'AA' 1 T T
'AB' 1 T F

file2 same as file1
<id> <val1> <test1> <test2>
'AA' 1 T T
'AB' 1 T T

Also the compare should be based on id. So it should look for line
starting with id 'AA' (for example) and then match the line so if in
second case.

so this is what I am looking for:
1. read both files.
2. read id of first line in file1 check if it matches with the same id
in file2.
3. repeat step 2 for all lines in file1.
4. return a percent of success to failure. ie if one line matches and
one lines does'nt then return 0.5 or 50%

I wrote a boolean version ..as a start

def getdata(f):
try:
f1 = open(f,'r')
data=[]
for eachline in f1.readlines():
data.append(re.split("",
re.sub('\n','',strip(re.split('\s\s+',eachline)[0]))))
return data
except IOError:
raise("Invalid File Input")

if __name__=='__main__':

data1 = getdata('file1')
data2 = getdata('file2')

if data1 == data2:
print "True"
else:
print "False"

hope I am clear...

Oct 12 '05 #1
5 3088
Note that the code i wrote wont do the compare based on id which i am
looking for..it just does a direct file to file compare..

Oct 12 '05 #2
Sounds a little like "homework", but I'll help you out.
There are lots of ways, but this works.

import sys
class fobject:
def __init__(self, inputfilename):
try:
fp=open(inputfilename, 'r')
self.lines=fp.readlines()
except IOError:
print "Unable to open and read inputfilename=%s" % inputfilename
sys.exit(3)

self.datadict={}
for line in self.lines:
line=line.strip()
line=line.strip("'")
key, values=line.split(' ',1)
self.datadict[key]=values

return

def keys(self):
return self.datadict.keys()

def compare(self, otherobject):
keys=otherobject.keys()
match=0
for key in keys:
if self.datadict[key] == otherobject.datadict[key]: match+=1

return float(match)/float(len(keys))

if __name__=="__main__":
f1=fobject(r'f:\syscon\python\zbkup\f1.txt')
f2=fobject(r'f:\syscon\python\zbkup\f2.txt')
print f1.compare(f2)
Larry Bates
PyPK wrote:
I have two files
file1 in format
<id> <val1> <test1> <test2>
'AA' 1 T T
'AB' 1 T F

file2 same as file1
<id> <val1> <test1> <test2>
'AA' 1 T T
'AB' 1 T T

Also the compare should be based on id. So it should look for line
starting with id 'AA' (for example) and then match the line so if in
second case.

so this is what I am looking for:
1. read both files.
2. read id of first line in file1 check if it matches with the same id
in file2.
3. repeat step 2 for all lines in file1.
4. return a percent of success to failure. ie if one line matches and
one lines does'nt then return 0.5 or 50%

I wrote a boolean version ..as a start

def getdata(f):
try:
f1 = open(f,'r')
data=[]
for eachline in f1.readlines():
data.append(re.split("",
re.sub('\n','',strip(re.split('\s\s+',eachline)[0]))))
return data
except IOError:
raise("Invalid File Input")

if __name__=='__main__':

data1 = getdata('file1')
data2 = getdata('file2')

if data1 == data2:
print "True"
else:
print "False"

hope I am clear...

Oct 12 '05 #3
Not for homework. But anyway thanks much...

Oct 13 '05 #4
PyPK wrote:
I have two files
file1 in format
<id> <val1> <test1> <test2>
'AA' 1 T T
'AB' 1 T F

file2 same as file1
<id> <val1> <test1> <test2>
'AA' 1 T T
'AB' 1 T T

Also the compare should be based on id. So it should look for line
starting with id 'AA' (for example) and then match the line so if in
second case.


See the recent thread with subject line "List performance and CSV".
Oct 14 '05 #5
but what if
case 1:
no.of keys in f1 > f2 and
case2:
no.of keys in f1 < f2.
Should'nt we get 1.1 if case 1 and 0.9 if case 2?? it errors of with a
keyerror.?

Oct 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Raseliarison nirinA | last post by:
hi all, i found an unanswered question at http://www.faqts.com/knowledge_base/index.phtml/fid/538 with possible response below. i've tried to send it at faqt.python but can't figure out how to...
0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
2
by: zjut | last post by:
want to add a string to the file and the file is sort by letter! for examply: the follow file is a big file! ////////////////////// abort black cabbage dog egg fly ////////////////////
2
by: RitaG | last post by:
Hi. In a VB.Net program I use File.Copy to copy some zipped (and text) files from one server to another. I'm required by management to do a file compare after the copy has completed. Is the...
46
by: dawn | last post by:
Hi all, I am now working on a C program under Unix. The requirement for the program is that: A file name is passed to program as a parameter. The program will Find files under a specified...
0
by: Sunit Joshi | last post by:
Hello All I'm trying to figure out how to do this the best way. Basically, I need to compare DateTime of files across TimeZones. The situation is like this: 1. I have a database say in Korea...
18
by: Torben Laursen | last post by:
Hi Can anyone tell me what is the short-cut to switch between a .h and .cpp file, thanks Torben Laursen ---
1
by: Roy | last post by:
Hello, As a part of my application,I need to compare files generated everyday.I need not need to compare the file contents.I just need to compare the file size,the line count.The files are named...
8
by: Perl Beginner | last post by:
I am new to Perl and new to this site. I have the same question that I keep seeing, but not finding an answer…why doesn’t the compare function work? I’ve been going at this for a while. My code is...
3
by: blunt | last post by:
right the program is nearly complete just a couple of little tweaks and i should have it. The purpose of this program is to write config files for colubrius wireless access points but it's falling...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.