Jan-11-2019, 05:42 PM
Hello everyone,
Please don't be too harsh on me, I am a total noob to python. I'm sure my code is pretty bad but I am still learning. I’ve done a considerable amount of research on python from this site and many others but I have wracked my brain trying to figure this out.
I'll do my best to convey what I am trying to do. I‘m pulling some weather data from this site. I am trying to use python to parse this data. Once the data is found, I need to print it to another file. As you can see in my code, it reports that it has found my search term but it does not tell me what line number it is.
The data I need can vary +- 4 lines but it's the 14 consecutive lines I need to have that are just after my search term. That search term is always a constant. Without the line number I can't finish this little project of mine.
Any and all advice, help would be greatly appreciated!
I hope this makes sense and doesn't sound like the ramblings of a 60 yr. old man :) Below is the code I have wrote thus far.
Please don't be too harsh on me, I am a total noob to python. I'm sure my code is pretty bad but I am still learning. I’ve done a considerable amount of research on python from this site and many others but I have wracked my brain trying to figure this out.
I'll do my best to convey what I am trying to do. I‘m pulling some weather data from this site. I am trying to use python to parse this data. Once the data is found, I need to print it to another file. As you can see in my code, it reports that it has found my search term but it does not tell me what line number it is.
The data I need can vary +- 4 lines but it's the 14 consecutive lines I need to have that are just after my search term. That search term is always a constant. Without the line number I can't finish this little project of mine.
Any and all advice, help would be greatly appreciated!
I hope this makes sense and doesn't sound like the ramblings of a 60 yr. old man :) Below is the code I have wrote thus far.
# my first attempt at python # lines = [] with open ('nws.dat', 'rt') as infile: for line in infile: lines.append(line.rstrip('\n')) for element in lines: print(element, end='') enter enumerate(lines) for linenum, line in enumerate(lines): print (linenum, line) import re pattern = re.compile(r"\bE\w*v\b") index = 0 str = lines[linenum] substr = "Elev" pat = re.compile(r"\bE\w*v\b") while index < len(str): index = str.find(substr, index) if pat.search(substr) != None: print (" ", "Found it!") if index == -1: break print("Line: ", linenum, "Index: ", index) index += len(substr) enter enter Found it!