【发布时间】:2016-11-01 21:52:36
【问题描述】:
for line in open('transactions.dat','r'):
item=line.rstrip('\n').split(',')
custid=item[2]
amt=item[4]
if custid in cust:
amt1=int(cust[custid])+int(amt)
cust[custid]=amt1
else:
cust[custid]=[amt]
我正在尝试检查客户 ID 是否已经存在于字典中,然后只需在该客户中添加以前的金额和新金额。否则,将该金额添加到列表中的新位置。但我收到错误:
Traceback (most recent call last):
File "<pyshell#74>", line 7, in <module>
amt1=int(cust[custid])+int(amt)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
一些交易数据是这样的:
101300101,2016-09-03,376248582,1013,10.92
109400132,2016-09-03,391031719,1094,36.72
136100107,2016-09-03,391031719,1361,28.77
【问题讨论】:
-
用
cust[custid]=amt替换cust[custid]=[amt]? -
不,它不起作用。问题在我的代码的第 7 行
-
第 7 行的错误是 Isamael 指出的错误的后果。
-
不,它不起作用。它给出了这个错误:amt1=int(cust[custid])+int(amt) TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
-
@Kam,它给你这个错误即使你做了我建议的改变?
标签: python dictionary