My code reads data from NOAA’s buoy service. The url is in .txt format which my code then uses to assign variables based on their position in the list. Typically the list which I have assigned the variable “lines” will contain the following info:
['Station CAMM2', "38° 34.4' N 76° 4.1' W", '', '3:00 pm EDT', '1900 GMT 05/22/24', 'Wind: SSW (210°), 9.9 kt', 'Gust: 15.0 kt', 'Pres: 29.96 falling', 'Air Temp: 82.8 °F', 'Water Temp: 68.5 °F', '']
I want to assign variables like this:
when = lines[3]
wind = lines[5]
gust = lines[6]
pressure = lines[7]
air_temp = lines[8]
water_temp = lines[9]
However, sometimes one or more of these elements is missing from text feed. For example, air_temp
will be missing from the feed, so water_temp = lines[9]
will result in out-of-index error. I know this is basic, but how can I assign variables from a list that doesn’t always have the same number of elements?
2