我有一本字典,其中包含以下字典列表:
output = { "Facebook": [
{ "prefix": "5.45.32.0\/22", "exact": true },
{ "prefix": "185.160.58.0\/23", "exact": false, "greater-equal": 24, "less-equal": 24 },
{ "prefix": "5.57.248.0\/21", "exact": true },
{ "prefix": "5.206.208.0\/20", "exact": true },
{ "prefix": "185.54.94.0\/23", "exact": false, "greater-equal": 24, "less-equal": 24 },
{ "prefix": "31.193.104.0\/21", "exact": true },
{ "prefix": "213.233.192.0\/18", "exact": true },
{ "prefix": "217.26.96.0\/20", "exact": true }
] }
for line in output:
print(line['prefix")
我想遍历整个过程,像这样打印每行:
5.45.32.0, True
185.160.58.0, 24
例如,从第一项中删除\/22
有人可以帮助我了解我如何使其工作吗?
答案 0 :(得分:0)
output = { "Facebook": [
{ "prefix": "5.45.32.0\/22", "exact": 'true' },
{ "prefix": "185.160.58.0\/23", "exact": 'false', "greater-equal": 24, "less-equal": 24 },
{ "prefix": "5.57.248.0\/21", "exact": 'true' },
{ "prefix": "5.206.208.0\/20", "exact": 'true' },
{ "prefix": "185.54.94.0\/23", "exact": 'false', "greater-equal": 24, "less-equal": 24 },
{ "prefix": "31.193.104.0\/21", "exact": 'true' },
{ "prefix": "213.233.192.0\/18", "exact": 'true' },
{ "prefix": "217.26.96.0\/20", "exact": 'true'}
] }
for item in output['Facebook']:
if item['exact'] == 'true':
print ("{}, {}".format(item['prefix'].split('\/')[0], item['exact']))
else:
print ("{}, {}".format(item['prefix'].split('\/')[0], item['greater-equal']))
输出:
5.45.32.0, true
185.160.58.0, 24
5.57.248.0, true
5.206.208.0, true
185.54.94.0, 24
31.193.104.0, true
213.233.192.0, true
217.26.96.0, true
或:
注意:如果您要像字符串或True一样使用'true',请注意: like 布尔值
output = { "Facebook": [
{ "prefix": "5.45.32.0\/22", "exact": True },
{ "prefix": "185.160.58.0\/23", "exact": False, "greater-equal": 24, "less-equal": 24 },
{ "prefix": "5.57.248.0\/21", "exact": True },
{ "prefix": "5.206.208.0\/20", "exact": True },
{ "prefix": "185.54.94.0\/23", "exact": False, "greater-equal": 24, "less-equal": 24 },
{ "prefix": "31.193.104.0\/21", "exact": True },
{ "prefix": "213.233.192.0\/18", "exact": True },
{ "prefix": "217.26.96.0\/20", "exact": True }
] }
for item in output['Facebook']:
if item['exact'] == True:
print ("{}, {}".format(item['prefix'].split('\/')[0], item['exact']))
else:
print ("{}, {}".format(item['prefix'].split('\/')[0], item['greater-equal']))
答案 1 :(得分:0)
这应该有效。
output = { "Facebook": [
{ "prefix": "5.45.32.0\/22", "exact": True },
{ "prefix": "185.160.58.0\/23", "exact": False, "greater-equal": 24, "less-equal": 24 },
{ "prefix": "5.57.248.0\/21", "exact": True },
{ "prefix": "5.206.208.0\/20", "exact": True },
{ "prefix": "185.54.94.0\/23", "exact": False, "greater-equal": 24, "less-equal": 24 },
{ "prefix": "31.193.104.0\/21", "exact": True },
{ "prefix": "213.233.192.0\/18", "exact": True },
{ "prefix": "217.26.96.0\/20", "exact": True }
] }
#Iterate through all dictionaries
for dct in output['Facebook']:
#Get value of prefix and exact
prefix = dct['prefix'].split('\/')[0]
exact = dct['exact']
#If exact is present, use it,
if exact:
print('{} {}'.format(prefix, exact))
#Else use greater-equal key
else:
gr_eq = dct.get('greater-equal')
print('{} {}'.format(prefix, gr_eq))
输出将为
5.45.32.0 True
185.160.58.0 24
5.57.248.0 True
5.206.208.0 True
185.54.94.0 24
31.193.104.0 True
213.233.192.0 True
217.26.96.0 True
还要注意,"exact": true
会引发语法错误,而应该执行"exact": True
,因为布尔值由True
而不是true
表示。
答案 2 :(得分:0)
output = { "Facebook": [
{ "prefix": "5.45.32.0\/22", "exact": True },
{ "prefix": "185.160.58.0\/23", "exact": False, "greater-equal": 24, "less-equal": 24 },
{ "prefix": "5.57.248.0\/21", "exact": True },
{ "prefix": "5.206.208.0\/20", "exact": True },
{ "prefix": "185.54.94.0\/23", "exact": False, "greater-equal": 24, "less-equal": 24 },
{ "prefix": "31.193.104.0\/21", "exact": True },
{ "prefix": "213.233.192.0\/18", "exact": True },
{ "prefix": "217.26.96.0\/20", "exact": True }
] }
facebook_apis = output['Facebook']
for facebook_api in facebook_apis:
# print(facebook_api['prefix'])
try:
print(str(facebook_api['prefix'].split("\/")[0]) + ', ' + str(facebook_api['greater-equal']))
except:
print(str(facebook_api['prefix'].split("\/")[0]) + ", " + str(facebook_api["exact"]))
输出将是:
5.45.32.0, True
185.160.58.0, 24
5.57.248.0, True
5.206.208.0, True
185.54.94.0, 24
31.193.104.0, True
213.233.192.0, True
217.26.96.0, True