help with loops
Catalina Scott A Contr AFCA/EVEO
scott.catalina.ctr at scott.af.mil
Mon Mar 6 11:15:27 EST 2006
I sent that last email out too soon. It appeared to be working but it is
only comparing the last line of file1 with all lines of file2.
Any idea how to make this loop work to compare every line of file1 with
every line of file2?
for line1 in file1.readlines():
list1 = line1.split(",")
for line2 in file2.readlines():
list2 = line2.split(",")
if (list1[3] == list2[6] and list1[4] == list2[8]):
print line
Thanks,
Scott
-----Original Message-----
From: Catalina Scott A Contr AFCA/EVEO
Sent: Monday, March 06, 2006 9:48 AM
To: 'Tim Chase'
Cc: python-list at python.org
Subject: RE: help with loops
I was trying to compare each line of file1 with each line of file2. Your
solution worked perfectly.
Thanks!
Scott
-----Original Message-----
From: Tim Chase [mailto:python.list at tim.thechases.com]
Sent: Monday, March 06, 2006 9:17 AM
To: Catalina Scott A Contr AFCA/EVEO
Cc: python-list at python.org
Subject: Re: help with loops
> I have two files and I want to pull out lines from file 2
> if certain conditions are found if file 1.
Well, I'm not quite sure from your description of the
problem if you want to compare every line in file1 against
every line in file2, or if you just want to compare line1 of
file1 with line1 of file2; line2 of file1 with line2 of file2...
For the former, you'd use something like
for line1 in file1.readlines():
list1 = line1.split(",")
for line2 in file2.readlines():
list2 = line2.split(",")
if (list1[3] == list2[6] and list1[4] == list2[8]):
print line
For the latter, you have to consider the case where one file
is longer than the other, unless you happen to know that
they're the same length. If they are, or you just want the
min length of whichever is shorter, you could do something like
for line1,line2 in zip(file1.readlines(), file2.readlines()):
list1 = line1.split(",")
list2 = line2.split(",")
if list1[3] == list2[6] and list1[4] == list2[8]:
print line
Just a few thoughts you might try tinkering with to see if
they do something close to what you want.
-tkc
More information about the Python-list
mailing list