Reputation: 23
I want to extract all MaxTemp for year 2010
Year Month Day MaxTemp MinTemp
2010 1 1 -19.9 -37.2
2010 1 2 -13.8 -20
2010 1 3 -13.1 -15.9
2010 1 4 -12 -22.3
2010 1 5 -11.8 -14.4
2010 1 6 -14.3 -32.5
2010 1 7 -28.2 -37.3
2011 1 8 -21.9 -31.3
2011 1 9 -7.4 -22.8
2011 1 10 -6.6 -15.3
2011 1 11 -0.7 -15.2
2011 1 12 4.3 -5.8
my current code is
with open('data/'+file_name, "rU") as files:
val = list(csv.reader(files))[1:]
specific output
[['2010', '01', '01', '9.6', '5.8'], ['2010', '01', '02', '8.6', '6.2'], ['2010', '01', '03', '8.8', '6.0'], ['2010', '01', '04', '6.8', '5.6'], ['2010', '01', '05', '9.0', '4.4'], ['2010', '01', '06', '8.1', '1.0'], ['2010', '01', '07', '6.3', '0.9'], ['2010', '01', '08', '7.8', '4.2'], ['2010', '01', '09', '10.4', '7.5'], ['2010', '01', '10', '11.5', '7.9'], ['2010', '01', '11', '11.9', '8.9']]
this extract whole csv without header. How can i accomplish the desired task of extracting all MaxTemp for only year 2010. Year I can pass as an argument. Thanks much.
Upvotes: 2
Views: 65
Reputation: 5115
You can do it like this, then data
will be a list of MaxTemps from 2010:
with open('data/'+file_name, "rU") as files:
data = list()
for line in csv.reader(files):
if int(line[0])==2010:
data.append(line[3])
Better would be to use something like pandas and filter the data that way. If it's not a huge amount of data you should be ok this way though.
Upvotes: 0
Reputation: 387
Here you go:
maxList = list()
for row in val:
rowInStr = ''.join(row)
rowSplitList = rowInStr.split(" ")
if rowSplitList[0] == "2010":
rowSplitList = filter(lambda a: a!='', rowSplitList)
maxList.append(rowSplitList[-2])
Output:
['-19.9', '-13.8', '-13.1', '-12', '-11.8', '-14.3', '-28.2', '-21.9', '-7.4', '-6.6', '-0.7', '4.3']
Please use proper indentation. I prefer tab for indentation. Hope this will help.
Upvotes: 0
Reputation: 2639
As rofls said, but with comprehensions
with open('data/'+file_name, "rU") as files:
data = [x[3] for x in csv.reader(files) if str(x[0]) == '2010']
Upvotes: 1