I am trying to iterate over a list that contains temperatures. Appending the data/temperatures from a csv file to a list data structure was a not an issue. The problem occures when i want to count the occurences of temperatures above 6. I was print-testing.
I get an error due to the first element in the csv.column is a variable "SDK", which denotes the temperatures. How can 'bypass' the first value ("SDK") in that column? As I only want/need to iterate over the integers.
The code:
def sunshine(file):
with open(file,'r') as csv_file:
lines = csv_file.readlines()
temperaturesDays = []
for line in lines:
data = line.split(',')
temperaturesDays.append(data[8])
return temperaturesDays
#print(temperaturesDays)
daily_sunshine_duration = sunshine('berlin.csv')
#print(daily_sunshine_duration) #works, print temps
for i in daily_sunshine_duration:
if i < 6:
print(i)
Within data[8]
you do not only have integer values. You could either go for try...except
as in:
temperaturesDays = []
for line in lines:
data = line.split(',')
try:
temperaturesDays.append(int(data[8]))
except:
pass
return temperaturesDays
... or check/cast it elsewhere (e.g. float(...)
, isinstance(...)
).
Without real sample values it is hard to guess though.