Python csv:找出具有最大值的行
我一定错过了一些东西,但我很难找出最大值并使用 csv.DictReader() 函数打印它们。
csv 文件类似于(我已经剪切了字段和行,因为行对于这种格式来说太宽):(
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
希望我得到了所有字段,我必须对原始文件使用剪切。)
我读了这个文件但当
tanks = csv.DictReader(open('tanks.csv', 'r'), delimiter = ';')
我试图找出例如哪一行在哪一列中具有最大值时,我似乎无法阅读字典。我的尝试看起来像这样:
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] + ")"
但我知道
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
我怎样才能完成这个?我想这样做以便于添加新字段。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在tank = list(itertools.islice(tanks,1,2))中,您将tank定义为列表。
best_tanks 是一个字典,其值为列表坦克。
在您的
if
中,您将 best_tanks 中的列表替换为现在是字典的变量 Tank,因此稍后可以使用best_tanks[field][field]
。但如果不执行
if
if,best_tanks中的元素仍然会有一个列表,导致best_tanks[field]
是一个列表,就会出现错误< code>TypeError: 当您尝试使用字符串字段访问元素时,列表索引必须是整数,而不是 strIn
tank = list(itertools.islice(tanks,1,2))
you define tank as a list.best_tanks is a dict with the values being the list tank.
In your
if
you replace the lists in best_tanks with the variable tank that now is a dict, so its ok to later usebest_tanks[field][field]
.But if the
if
if is not executed, the element in best_tanks will still have a list, resulting inbest_tanks[field]
being a list, which will result in the errorTypeError: list indices must be integers, not str
when you try to access the element with the string field