python - Iterating over line index and printing the full line -


i have (kind of) big text file in have find specific lines tells current action. of lines have find line (usually 4-6 lines beneath) tells information of said action. current script looks this:

filelines = file.readlines() nrlines = enumerate(filelines) i,line in nrlines:     if "tiltarmaxis::stategoingtoalmostup" in line:         print "%d - %s" % (i, line)         i,line in range(i, i+7):             if "axis::statepositionmode:" in line:                 print "%d - %s" % (i, line) 

it works in first not in second, get:

4520 - "2014-11-13 08:13:43",t:305310  tiltarmaxis - changing state tiltarmaxis::stateatdown -> tiltarmaxis::stategoingtoalmostup 

but then:

for i,line in range(i, i+7): typeerror: unpack non-sequence 

so basically, want fist index every line enumerate, find lines "tiltarmaxis::stategoingtoalmostup" in them , print. in next 6 lines "axis::statepositionmode:" , if such line occur (if not, skip it); print , go next "tiltarmaxis::stategoingtoalmostup".

is there more effective way of doing this? please help

edit: removed args,

range(i, i+7): 

still same error

range(i, i+7) simple sequence of integers between i , i+7. need :

nrlines = enumerate(file) i,line in nrlines:     if "tiltarmaxis::stategoingtoalmostup" in line:         print "%d - %s" % (i, line)         j in range(7):             i, line = next(nrlines)  # use next on iterator !             if "axis::statepositionmode:" in line:                 print "%d - %s" % (i, line) 

btw, should protect next try: ... except stopiteration: ... avoid error if reach end of file during inner loop. cannot it, because not know want in except case :-)


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -