如何将键/值对插入字典列表中包含的每个字典

2024-10-02 10:18:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

对于每个泄露的帐户,我想插入密钥/值对“Email”:在返回的字典列表中的每个字典中,然后将其导出到CSV文件。我当前在插入键/值对时遇到问题。你知道吗

为便于阅读,返回的数据以换行符分隔:

[{'Name': 'BTSec', 'Title': 'Bitcoin Security Forum Gmail Dump', 'Domain': 'forum.btcsec.com', 'BreachDate': '2014-01-09', 'AddedDate': '2014-09-10T20:30:11Z', 'ModifiedDate': '2014-09-10T20:30:11Z', 'PwnCount': 4789599, 'Description': 'In September 2014, a large dump of nearly 5M usernames and passwords was posted to a Russian Bitcoin forum. Whilst commonly reported as 5M "Gmail passwords", the dump also contained 123k yandex.ru addresses. Whilst the origin of the breach remains unclear, the breached credentials were confirmed by multiple source as correct, albeit a number of years old.', 'LogoType': 'svg', 'DataClasses': ['Email addresses', 'Passwords'], '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': 'In late 2016, a huge list of email address and password pairs appeared in a "combo list" referred to as "Exploit.In". The list contained 593 million unique email addresses, many with multiple different passwords hacked from various online systems. The list was broadly circulated and used for "credential stuffing", that is attackers employ it in an attempt to identify other online systems where the account owner had reused their password. For detailed background on this incident, read Password reuse, credential stuffing and another billion records in Have I been pwned.', 'LogoType': 'svg', 'DataClasses': ['Email addresses', 'Passwords'], 'IsVerified': False, 'IsFabricated': False, 'IsSensitive': False, 'IsRetired': False, 'IsSpamList': False}

{'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': 'In May 2016, LinkedIn had 164 million email addresses and passwords exposed. Originally hacked in 2012, the data remained out of sight until being offered for sale on a dark market site 4 years later. The passwords in the breach were stored as SHA1 hashes without salt, the vast majority of which were quickly cracked in the days following the release of the data.', 'LogoType': 'svg', 'DataClasses': ['Email addresses', '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

Tags: andoftheinfalsefordataemail
1条回答
网友
1楼 · 发布于 2024-10-02 10:18:22

出现此错误是因为您使用元素而不是索引来迭代列表data,因此i将是每次迭代时data的每个元素,并且在调用data[i]时传递的是dict而不是索引。你知道吗

要解决这个问题,只需将for语句修改为for i in range(len(data))。你知道吗

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

相关问题 更多 >

    热门问题