我有以下字典:
{'https://www.youtube.com/sw.js_data': ['HTTP/2 200 OK',
'Content-Type: application/json; charset=utf-8',
'X-Content-Type-Options: nosniff',
'Cache-Control: no-cache, no-store, max-age=0, must-revalidate',
'Pragma: no-cache',
'Expires: Mon, 01 Jan 1990 00:00:00 GMT',
'Date: Mon, 14 Mar 2022 17:59:34 GMT',
'Content-Disposition: attachment; filename="response.bin"; filename*=UTF-8\'\'response.bin',
'Strict-Transport-Security: max-age=31536000',
'X-Frame-Options: SAMEORIGIN',
'Cross-Origin-Opener-Policy-Report-Only: same-origin; report-to="ATmXEA_XZXH6CdbrmjUzyTbVgxu22C8KYH7NsxKbRt94"',
'Permissions-Policy: ch-ua-arch=*, ch-ua-bitness=*, ch-ua-full-version=*, ch-ua-full-version-list=*, ch-ua-model=*, ch-ua-platform=*, ch-ua-platform-version=*',
'Accept-Ch: Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Full-Version, Sec-CH-UA-Full-Version-List, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version',
'Server: ESF',
'X-Xss-Protection: 0',
'Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'],
'https://www.google.com/client_204?&atyp=i&biw=1440&bih=849&dpr=1.5&ei=Z4IvYpTtF5LU9AP1nIOICQ': ['HTTP/2 204 No Content',
'Content-Type: text/html; charset=UTF-8',
'Strict-Transport-Security: max-age=31536000',
"Content-Security-Policy: object-src 'none';base-uri 'self';script-src 'nonce-9KQUw4dRjvKnx/zTrOblTQ==' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/cdt1",
'Bfcache-Opt-In: unload',
'Date: Mon, 14 Mar 2022 17:59:10 GMT',
'Server: gws',
'Content-Length: 0',
'X-Xss-Protection: 0',
'X-Frame-Options: SAMEORIGIN',
'Set-Cookie: 1P_JAR=2022-03-14-17; expires=Wed, 13-Apr-2022 17:59:10 GMT; path=/; domain=.google.com; Secure; SameSite=none',
'Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"']}
我很難在這本字典中呼叫和搜索這些鍵和值。我正在嘗試撰寫一個 for 回圈或串列理解,它將搜索字典中的每個值并查找特定文本(例如“Strict-Transport-Security”)。回圈后,我希望它列印密鑰,然后列印搜索結果。因此,此字典所需的示例輸出將是:
https://www.youtube.com/sw.js_data: Strict-Transport-Security missing
https://www.google.com/client_204?&atyp=i&biw=1440&bih=849&dpr=1.5&ei=Z4IvYpTtF5LU9AP1nIOICQ: Strict-Transport-Security present
希望這是有道理的。假設可以做到,但我很難做到這一點。謝謝!
uj5u.com熱心網友回復:
header = 'Strict-Transport-Security'
for url in mydictionary:
if any(s.startswith(header) for s in mydictionary[url]):
print(f"{header} found for {url}")
else:
print(f"{header} missing for {url}")
uj5u.com熱心網友回復:
我想與您分享另一種方法 - 您可以將字串串列轉換為字典
def parse_list(list_of_str):
res = {}
for element in list_of_str:
try:
x = element.split(":")
res[x[0]] = x[1].strip()
except IndexError:
continue
return res
parsed = {url: parse_list(l) for url, l in your_dict.items()}
現在您可以將其用作普通字典:
for url, subdict in parsed.items():
print(url, "present" if "Strict-Transport-Security" in subdict else "missing")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449384.html
下一篇:for回圈中的賦值
