[英]TypeError: list indices must be integers or slices, not str WITH API
result = [{'line': 'johannsasuke@gmail.com:42ab89', 'sources': ['Taringa.net'], 'last_breach': '2017-08'}, {'line': 'johannsasuke@gmail.com:PEIN12345', 'sources': ['Evony.com'], 'last_breach': '2016-07'}, {'line': 'johannsasuke@gmail.com:sasuke12345', 'sources': ['Animoto.com', 'Collection 1', 'xSplit'], 'last_breach': '2019-01'}, {'line': 'johannsasuke@gmail.com', 'sources': ['xSplit'], 'last_breach': '2013-11', 'email_only': 1}]
for x in result:
if result['email_only']==1:
pass
else:
print(result['line'])
I'm trying to print the combos that are released with this api but am getting TypeError: list indices must be integers or slices, not str.我正在尝试打印与此 api 一起发布的组合,但出现 TypeError:列表索引必须是整数或切片,而不是 str。 Please help!
请帮忙!
result
is a list, so you probably meant x
, as this is your item: result
是一个列表,因此您的意思可能是x
,因为这是您的项目:
for x in result:
if x.get("email_only") == 1:
continue
print(x["line"])
Prints:印刷:
johannsasuke@gmail.com:42ab89
johannsasuke@gmail.com:PEIN12345
johannsasuke@gmail.com:sasuke12345
NOTE: I used x.get()
, because it returns None
if key email_only
doesn't exist in the dictionary (not throwing exception).注意:我使用
x.get()
,因为如果字典中不存在密钥email_only
(不抛出异常),它会返回None
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.