Wednesday, 21 August 2013

Increment and loop till the end of the file(EOF)?

Increment and loop till the end of the file(EOF)?

Consider the following code:
history_begins = 1; history_ends = 5000
historyjobs = []; targetjobs = []
listsub = []; listrun = [];
def check(inputfile):
f = open(inputfile,'r')
lines = f.readlines()
for line in lines:
job = line.split()
if( int(job[0]) < history_ends ):
historyjobs.append(job)
# appends all lines to the historyjobs list from inputfile
# whose col1 value is < 5000
else:
targetjobs.append(job)
# appends all lines to the historyjobs list from inputfile
# whose col1 value is > 5000
print targetjobs[0] # To print the first list in targetjobs list
j = 0
for i in range(len(historyjobs)):
if( (int(historyjobs[i][3]) == int(targetjobs[j][3]))
and (int(historyjobs[i][4]) == int(targetjobs[j][4]))
and (int(historyjobs[i][5]) == int(targetjobs[j][5])) ):
# comparing the item 3,4,5 of all the lists in historyjobs list
# with the item 3,4,5 of the first list in targetjobs list
listsub.append(historyjobs[i][1])
listrun.append(historyjobs[i][2])
print listsub
print listrun
def main():
check('newfileinput')
if __name__ == '__main__':
main()
The result I obtained was:
# output snipped for brevity
['5000', '1710390', '930', '8', '9', '2']
['767220', '769287', '770167', ... , '1710375']
['2717', '184', '188', '163', ... , '977']
Now what I need to do is to increment the history_ends value, i.e.:
history_ends = 5000 initial assignment and prints the results
next history_ends = 5001
next history_ends = 5002-------------------till the end of the input file
so that the result I need is:
['5000', '1710390', '930', '8', '9', '2'] # when history_ends=5000
['767220', '769287', '770167', ... , '1710375']
['2717', '184', '188', '163', ... , '977']
['5001', '1710554', '4348', '8', '9', '2'] # when history_ends=5001
['767220', '769287', '770167', ... , '1710375', '1710390']
['2717', '184', '188', '163', ... , '977', '930']
['5002', '1710791', '18047', '32', '137', '3'] # when history_ends=5002
['1302665', '1364654', '1385017']
['17285', '61465', '17961']
... # rest of file follows similar structure
Can any one suggest how I can modify my code to be able to do this in
python? It would be helpful.

No comments:

Post a Comment