Python Forum
Python assisstance for a beginner please
Thread Rating:
  • 4 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python assisstance for a beginner please
#1
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:
	    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!
Reply
#2
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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

51°F

11°C

*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
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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

Rick
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
An example with BeautifulSoup that @buran mention.
from bs4 import BeautifulSoup
import requests

url = 'https://forecast.weather.gov/MapClick.php?lat=32.47318000000007&lon=-100.40478999999999#.XC2EbM1MFEa/'
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'):
    print(weathet_data.text.strip())
Output:
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020