Bottom Page

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!

Quote
#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?
Quote
#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
Quote
#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.
Quote
#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
Quote
#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
Quote
#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
buran likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Beginner python password program jakobscheffler 4 94 Yesterday, 08:14 PM
Last Post: Alfalfa
  Absolute Beginner Python Question matchamochi7 5 289 Nov-02-2018, 12:09 PM
Last Post: wookie
  Absolute Beginner to Python dthomp54 5 307 Oct-09-2018, 03:00 AM
Last Post: dthomp54
  Setting up Python on a Mac - Beginner BrowneyesPage1979 1 209 Aug-30-2018, 10:41 PM
Last Post: snippsat
  Very beginner but please help! thurpe 2 271 Jul-31-2018, 04:04 PM
Last Post: thurpe
  Beginning of Beginner Help: Should I start with Python? appdevelnewb 2 394 Jul-23-2018, 11:17 PM
Last Post: appdevelnewb
  Beginner Problem python 2.7 Jonathan_levy 2 365 Jul-04-2018, 08:46 PM
Last Post: ichabod801
  Beginner Problem python 2.7 Jonathan_levy 2 326 Jul-03-2018, 11:58 AM
Last Post: gruntfutuk
  beginner Kyrophis 1 325 Jun-26-2018, 05:25 PM
Last Post: Larz60+
  problem about 'if' and 'for' from a python beginner yzjnpu 3 424 Jun-26-2018, 03:47 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)