python - TypeError : int() argument must be a string or a number, not 'list' 。程序在将 unicode 转换为 int 时出错

标签 python mysql dictionary unicode

所以这是我的程序片段,它给了我错误..

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/

相关文章:

mysql - 群组功能使用无效。但不使用组

php - 我需要用不同的值更新多行数据。我可以使用哪个 php 循环或代码来实现相同的功能?

java - Liquibase:组织变更日志的最佳实践

python - 使用 OrderedDictionary 对字谜进行分组 : TypeError issue

python - 如何在嵌套字典中查找重复或重复项?

python - 瀑布图中 "virtual third axis"的正确 matplotlib 转换是什么?

python - 付款方式 token 在 Braintree 中无效

python - 如何将子模块名称保留在 Python 包的 namespace 之外?

python - python 中的可瘫痪系统模型

dictionary - 将 Leaflet map 绑定(bind)到多个图层