Bottom Page

Thread Rating:
  • 4 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python assisstance for a beginner please
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.

# my first attempt at python
lines = []
with open ('nws.dat', 'rt') as infile:
	for line in infile:
	for element in lines:
	    print(element, end='')

	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 != None:
        print (" ", "Found it!")
    if index == -1:
    print("Line: ", linenum, "Index: ", index)
    index += len(substr)
 Found it!

in your code you parse file 'nws.dat'. where it comes from if you say you are puling data from the site? How does this file look like? What data you look for?
Hi buran and thanks for asking. The file contains 554 lines of data and I don't want to post all that here. This is what it looks like once python has enumerated it. The green highlight is my search term.

*Lat: *32.47°N*Lon: *100.47°W*Elev: *2379ft. <-----LINE 119 CAN VARY +- 4

Partly Cloudy <------I NEED DATA FROM HERE TO



*Humidity* 100%
*Wind Speed* S 20 mph
*Barometer* 30.01 in
*Dewpoint* 51°F (11°C)
*Visibility* 10.00 mi
*Wind Chill* 45°F (7°C)
*Last update* 11 Jan 3:55 am CST <------HERE
The reason I am asking is because it looks like you are scraping the site, yet the source is some dat file. If you are screen scraping the site - there are other tools.
Hey, well sorry for the confusion. The "nws.dat" file is simply a text .txt file. I have several thousand text files and I didn't want this file getting lost in all those files so I just changed the extension on the file when it was saved.
I am tying to automate the process so I don't have to manually copy and paste anything to anywhere. I am currently pulling this data from their site with a vbScript at the moment.

I am now trying to turn everything into python but I am still in the learning phase.
Thanks for your reply

when you say text file, is it just the visible text from the site or a html file.
you pull data with vbScript at the moment. Do you have the lat, lon for locations you are interested in? you can submit city name, and state then get number of locations that mach your search. then you can scrape data from the site using BeautifulSoup
An example with BeautifulSoup that @buran mention.
from bs4 import BeautifulSoup
import requests

url = ''
url_get = requests.get(url)
soup = BeautifulSoup(url_get.content, 'lxml')
weather = soup.find('div', id="current_conditions_detail")
for weathet_data in weather.find_all('tr'):
Humidity 94% Wind Speed S 18 G 24 mph Barometer 29.88 in Dewpoint 54°F (12°C) Visibility 10.00 mi Wind Chill50°F (10°C) Last update 11 Jan 2:35 pm CST
Rick Wrote:I am now trying to turn everything into python but I am still in the learning phase.
You can look at this.
Web-Scraping part-1
Web-scraping part-2
buran likes this post

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)