I must be missing something, but I have trouble with finding out the biggest values and printing them using the csv.DictReader() function.
The csv file is something like (I have cut the fields as well as rows because the rows are too wide for this format):
traverse;damage;hull_front;turret_back;penetration;full_name;tier;hull_back;turret_sides;type;hull_sides;turret_front
38;30;18;16;32;Light Tank MS-1;1;16;16;Light Tank;16;18
40;30;13;13;32;Light Tank BT-2;2;13;13;Light Tank;13;15
55;36;15;15;34;Light Tank T-26;2;15;15;Light Tank;15;15
(Hope I got all the fields, I had to use cut with the original file.)
I read this file with
tanks = csv.DictReader(open('tanks.csv', 'r'), delimiter = ';')
but when I try to found out e.g. which row has the biggest value in which column, I seem unable to read through the dictionary. My attempt looks like this:
def top_values(tanks):
tank = list(itertools.islice(tanks,1,2))
best_tanks = dict({'turret_front':tank, 'turret_sides':tank, 'turret_back':tank,
'hull_front':tank, 'hull_sides':tank, 'hull_back':tank,
'penetration':tank, 'damage':tank, 'traverse':tank})
fields = ['turret_front', 'turret_sides', 'turret_back',
'hull_front', 'hull_sides', 'hull_back',
'penetration', 'damage', 'traverse']
for tank in tanks:
for field in fields:
if tank[field] > best_tanks[field][field]:
best_tanks[field] = tank
print "Best tanks by values:\n"
for field in fields:
tank = best_tanks[field]
print field + ": " + tank['full_name'] + "(" + tank[field] + ")"
But I get
Traceback (most recent call last):
File "./wotalyzer.py", line 102, in <module>
main(sys.argv[1:])
File "./wotalyzer.py", line 98, in main
top_values(tanks)
File "./wotalyzer.py", line 44, in top_values
print field + ": " + tank['full_name'] + "(" + tank[field] + ")"
TypeError: list indices must be integers, not str
How can I accomplish this? I want to do it like this to make it easy to add new fields.