File Parsing Question

Zentrader zentraders at gmail.com
Wed Sep 12 20:08:50 EDT 2007


> for line in inp.readlines():

If you are now using readlines() instead of readline(), then
a) it is only used once to read all data into a container
b) you can access each element/line by it's relative number

data=open(filename, "r").readlines()
for eachline in data :   (not readlines())

so try
print data[0] ## first rec
print data[9] ## 10th rec, etc

you can use
ctr = 0
for eachline in data:
   ##do something
   if ctr > 0:
      print "this line is", eachline          ## or data[ctr]
      print "prev_line = ", data[ctr-1]
   ctr += 1

or a slightly different way
stop = len(data)
ctr=0
while ctr < stop:
   ## do something
   if ctr > 0 :
      this_line = data[ctr]
      prev_line = data[ctr-1]
   ctr += 1

Sorry, I don't use file.seek() so can't help there




More information about the Python-list mailing list