In python, how do I search for a number in a text file and print a corresponding value when it's found? -


i have list of cam angles , displacements:

<ignored header> 0   3 1   3 2   6 3   9 4   12 5   15 6   18 7   21 8   24 9   27 10  30 ... 

how go searching angle , producing displacement @ angle?

it's not necessary store data @ given moment need 2 values.

i know isn't working, brief explanation of why , how improve appreciated i'm eager learn more

camangle = 140  camfile = open(camfileloc) line in camfile:     if line > 1:         if camangle in line:         print line 

thanks much

lauren

you had it:

camangle = 140  # context manager closes file automatically when leave block open(camfileloc, 'r') handle:     next(handle)  # skips header      line in handle:         # splits line on whitespace , converts each string         # integer. then, unpack 2 variables (a tuple)         angle, displacement = map(int, line.split())          if angle == camangle:             print displacement             break  # exits `for` loop     else:         # never broke out of loop, angle never found         print 'this angle not in file' 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -