【发布时间】:2011-09-13 01:55:28
【问题描述】:
我有
形式的数据00 154
01 72
02 93
03 202
04 662
05 1297
00 256
我希望遍历每一行并将第 1 列中的值设为键,将第 2 列中的值设为值
如果当前键已经存在,则以数学方式将第 2 列的新值添加到第 2 列的当前值。
试过这个:
search_result = searches.stdout.readlines()
for output in search_result:
a,b = output.split()
a = a.strip()
b = b.strip()
if d[a]:
d[a] = d[a] + b
else:
d[a] = b
得到了这个:
Traceback (most recent call last):
File "./get_idmanager_stats.py", line 25, in <module>
if d[a]:
KeyError: '00'
【问题讨论】:
标签: python dictionary add