2015-11-06 52 views
0

因此,这里是它给我的错误我的程序片断..类型错误:int()函数的参数必须是字符串或数字,而不是“list'.Program给错误而转换的unicode为int

bob_phones={} 
for lea in leadArray: 

    lead_id=(lea.lead_id).text 
    phone = (lea.phone_number).text 
    if phone not in bob_phones.keys(): 
     new={phone:[lead_id]} 
     bob_phones.update(new) 


for i in getPhonenumber(date): 

    if i not in bob_phones.keys(): 
     status="Not in boberdoo" 
     lead_id="Not found" 
     #c.writerow([date,i,status,lead_id]) 
     print str(date) + " | | " + i + " | | " + status + " | | " + lead_id 
    elif i in bob_phones.keys(): 
     status = "Found in boberdoo" 
     lead_id=int(bob_phones[i]) 
     #c.writerow([date,i,status,lead_id]) 
     print str(date) + " | | " + i + " | | " + status + " | | " + str(lead_id) 



#print missing_leads 
return missing_leads 

所以在else if语句中,我想用lead_id更新mysql表,但是它会提取但它不让我将它从unicode转换为int,因为我从字典中获取该值。它给我这个错误。

TypeError: int() argument must be a string or a number, not 'list'.Program giving error while converting unicode to int. 

任何人都可以建议我的替代方法。

感谢

+0

抛出的错误是什么? – George

+0

对不起,乔治,我只是更新了它。 – Shubham

回答

2

看吧:

if phone not in bob_phones.keys(): 
    new={phone:[lead_id]} 
    bob_phones.update(new) 

当你创建dict你作为一个列表(new={phone:[lead_id]})由于支架分配值。所以删除方括号,它应该工作。

另外,请注意,您在if后不需要elif语句。要么我在bob_phones.keys()(if分支)或不是,所以你应该只使用else。这并不会改变任何事情,只是关于逻辑的一点。

+1

哦谢谢你的回复JR,我打算用代码做别的事情,这就是为什么我加了elif,尽管谢谢你让我知道。 – Shubham

相关问题