I have a weather data file that has high temps, low temp, rainfall, etc. I need to open the file and return data based on year ranges from user input. User inputs a starting date and ending date then I put that data into a list that user can then search for highest (HIGHTEMP) or lowest temps (LOWTEMP) or highest rainfall (PRCP) in that sub-list of data of year ranges.
My date comparison is not working for some reason, therefore nothing ever shows up in the results. I am using from dateutil.parser import parse
to convert the strings into Date types for comparison.
Here is what I have so far:
def openFile():
begin = input("Enter your starting year in this format YYYY ")
end = input("Enter your ending year for weather data in this format YYYY ")
def parse_line(line):
FORMAT_MAP = {
# fieldname : (start, end)
"STATION": (0, 17),
"STATION_NAME": (18, 68)
"ELEVATION": (69, 79),
"LATITUDE": (80, 90),
"LONGITUDE": (91, 102),
"DATE": (102, 111),
"PRCP": (112, 120),
"TEMPMAX": (121, 129),
"TEMPMIN": (130, 140)
}
return {name: line[start:end].strip() for name, (start, end) in FORMAT_MAP.items()}
def iter_parse_file(f, begin, end):
# skip the first two header lines
next(f); next(f)
# convert string into Date with parse
begin = parse(begin)
end = parse(end)
for line in f:
print(line)
row = parse_line(line)
year = row["DATE"][:4]
#parsing the year string into a Date
year = parse(year)
print("The year is ", year)
print("begin is ", begin)
print("end is ", end)
########################
#### This is the part that is not working as desired. I get no results
if year > begin:
continue
elif year < end:
break
yield row
def gen(begin, end):
with open("/Users/jasontt/data.txt") as f:
rows = list(iter_parse_file(f, str(begin), str(end)))
for row in rows:
print("Date is {DATE} : With a Minimum Temperature of {TEMPMIN} - and a Maximum Temperature of {TEMPMAX}".format(**row))
Sample Data:
STATION STATION_NAME ELEVATION LATITUDE LONGITUDE DATE PRCP TEMPMAX TEMPMIN
----------------- -------------------------------------------------- ---------- ---------- ---------- -------- -------- -------- --------
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490101 0.00 44 27
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490102 0.00 42 25
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490103 0.15 46 30
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490104 0.03 41 30
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490105 1.14 46 37
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490106 0.00 51 40
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490107 0.00 57 36
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490108 0.00 56 45
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490109 0.00 66 42
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490110 0.00 70 51
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490111 0.03 59 45
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490112 0.04 48 38
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490113 0.00 52 36
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490114 0.00 56 36
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490115 0.00 49 31
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490116 0.00 68 28
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490117 0.00 63 50
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490118 0.04 53 42
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490119 0.01 63 38
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490120 0.00 45 28
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490121 0.97 35 28
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490122 0.29 60 34
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490123 0.14 47 38
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490124 0.01 72 38
GHCND:USW00013741 SPOKANE REGIONAL AIRPORT WA US 366.1 37.31667 -79.96667 19490125 0.05 66 49
Aucun commentaire:
Enregistrer un commentaire