所以这是我的程序片段,它给了我错误..
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.
有人可以建议我另一种方式吗?
谢谢
最佳答案
看这里:
if phone not in bob_phones.keys():
new={phone:[lead_id]}
bob_phones.update(new)
当您创建 dict
时,由于括号的缘故,您会将值指定为列表 (new={phone:[lead_id]}
)。因此,删除那些方括号,它应该可以工作。
另外,请注意,您的 if 之后不需要 elif 语句。 i 要么在 bob_phones.keys() (if
分支)中,要么不在 bob_phones.keys() 中,所以您应该只使用 else
。这并没有真正改变任何东西,只是改变了逻辑。
关于python - TypeError : int() argument must be a string or a number, not 'list' 。程序在将 unicode 转换为 int 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33570858/