【问题标题】:How can I insert a key/value pair into each dictionary contained in a list of dictionaries如何将键/值对插入字典列表中包含的每个字典中
【发布时间】:2018-10-15 18:08:55
【问题描述】:

我正在编写一个脚本来检查电子邮件地址列表,以查看它们是否被报告为已泄露。返回的是 json 数据,本质上是一个字典列表。

对于每个被入侵的帐户,我想将键/值对 "Email" 插入到返回的字典列表中的每个字典中,然后将其导出到 CSV 文件。我目前无法插入键/值对。

返回的示例数据以换行符分隔以提高可读性:

[{'Name': 'BTSec', 'Title': 'Bitcoin Security Forum Gmail Dump', 'Domain': 'forum.btcsec.com', 'BreachDate': '2014-01-09', ' AdditionalDate': '2014-09-10T20:30:11Z', 'ModifiedDate': '2014-09-10T20:30:11Z', 'PwnCount': 4789599, 'Description': '2014 年 9 月,大量近 500 万个用户名和密码被发布到俄罗斯比特币论坛。虽然通常报告为 500 万个“Gmail 密码”,但该转储还包含 123k yandex.ru 地址。虽然违规的起源仍不清楚,但多个来源确认违规凭证是正确的,尽管已有多年历史。','LogoType':'svg','DataClasses':['电子邮件地址','密码' ], 'IsVerified': True, 'IsFabricated': False, 'IsSensitive': False, 'IsRetired': False, 'IsSpamList': False}

{'Name': 'ExploitIn', 'Title': 'Exploit.In', 'Domain': '', 'BreachDate': '2016-10-13', 'AddedDate': '2017-05- 06T07:03:18Z', 'ModifiedDate': '2017-05-06T07:03:18Z', 'PwnCount': 593427119, 'Description': '在 2016 年底,大量电子邮件地址和密码对出现在“组合列表”简称为“Exploit.In”。该列表包含 5.93 亿个独特的电子邮件地址,其中许多具有从各种在线系统窃取的多个不同密码。该列表被广泛传播并用于“凭证填充”,即攻击者使用它来试图识别帐户所有者重复使用其密码的其他在线系统。有关此事件的详细背景,请阅读 Have I been pwned 中的密码重用、凭据填充和另外十亿条记录。 ':假,'IsFabricated':假,'IsSensitive':假,'IsRetired':假,'IsSpamList':假}

{'Name': 'LinkedIn', 'Title': 'LinkedIn', 'Domain': 'linkedin.com', 'BreachDate': '2012-05-05', 'AddedDate': '2016-05 -21T21:35:40Z', 'ModifiedDate': '2016-05-21T21:35:40Z', 'PwnCount': 164611595, 'Description': '2016 年 5 月,LinkedIn 暴露了 1.64 亿个电子邮件地址和密码。这些数据最初于 2012 年遭到黑客攻击,直到 4 年后在一个暗市网站上出售之前一直看不见。泄露中的密码存储为不加盐的 SHA1 哈希,其中绝大多数在数​​据发布后的几天内被迅速破解。','LogoType':'svg','DataClasses':['电子邮件地址' , 'Passwords'], 'IsVerified': True, 'IsFabricated': False, 'IsSensitive': False, 'IsRetired': False, 'IsSpamList': False}]

这是我当前的代码:

def main():
if address != "None":
    checkAddress(address)
elif filename != "None":
    email = [line.rstrip('\n') for line in open(filename)] # strip the newlines
    for email in email:
        checkAddress(email)
else:
    for email in lstEmail:
        checkAddress(email)

def checkAddress(email):
sleep = rate # Reset default acceptable rate
check = requests.get("https://" + server + "/api/v2/breachedaccount/" + email + "?includeUnverified=true",
             headers = headers,
             proxies = proxies,
             verify = sslVerify)
if str(check.status_code) == "404": # The address has not been breached.
    print (OKGREEN + "[i] " + email + " has not been breached." + ENDC)
    time.sleep(sleep) # sleep so that we don't trigger the rate limit
    return False
elif str(check.status_code) == "200": # The address has been breached!
    print (FAILRED + "[!] " + email + " has been breached!" + ENDC)
    data = (check.json())


    for i in data:
        data[i].append( [{'test':'test'}])
        print (i)
        print ('\n') # Temp \n for readability


    time.sleep(sleep) # sleep so that we don't trigger the rate limit
    return True

这是我目前遇到的错误:

[!] j.doe@gmail.com has been breached!
Traceback (most recent call last):
   File "hibp2csv.py", line 95, in <module>
     main()
File "hibp2csv.py", line 52, in main
   checkAddress(email)
File "hibp2csv.py", line 70, in checkAddress
     data[i].append( [{'test':'test'}])
TypeError: list indices must be integers or slices, not dict

【问题讨论】:

    标签: python json list dictionary


    【解决方案1】:

    您收到此错误是因为您使用元素而不是索引对列表 data 进行迭代,因此 i 将在每次迭代和调用 data[i] 时成为 data 的每个元素你传递的是字典而不是索引。

    要解决此问题,您只需将 for 语句修改为 for i in range(len(data))

    另外,使用dict.update({"key": "value"}) 更新字典。

    【讨论】:

    • 是的,我在发布后发现了整个列表的迭代......谢谢这很完美。
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多