用python脚本搜索json文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用python脚本搜索json文件相关的知识,希望对你有一定的参考价值。
我有以下tfstate文件(它有JSON结构)
{
"version": 3,
"terraform_version": "0.11.1",
"serial": 1,
"lineage": "4d050905-8f3d-46df-8bbb-8859c708abe7",
"modules": [
{
"path": [
"root"
],
"outputs": {
"id": {
"sensitive": false,
"type": "list",
"value": [
"34.244.138.72",
"34.249.95.222"
]
}
},
"resources": {
"aws_instance.win-example.0": {
"type": "aws_instance",
"depends_on": [
"aws_security_group.DJukes"
],
"primary": {
"id": "i-0f8c0fcb36f58947d",
"attributes": {
"ami": "ami-cc821eb5",
"associate_public_ip_address": "true",
"availability_zone": "eu-west-1a",
"disable_api_termination": "false",
"ebs_block_device.#": "0",
"ebs_optimized": "false",
"ephemeral_block_device.#": "0",
"iam_instance_profile": "",
"id": "i-0f8c0fcb36f58947d",
"instance_state": "running",
"instance_type": "t2.medium",
"ipv6_addresses.#": "0",
"key_name": "",
"monitoring": "false",
"network_interface.#": "0",
"network_interface_id": "eni-8dbf17be",
"placement_group": "",
"primary_network_interface_id": "eni-8dbf17be",
"private_dns": "ip-172-31-30-138.eu-west-1.compute.internal",
"private_ip": "172.31.30.138",
"public_dns": "ec2-34-244-138-72.eu-west-1.compute.amazonaws.com",
"public_ip": "34.244.138.72",
"root_block_device.#": "1",
"root_block_device.0.delete_on_termination": "true",
"root_block_device.0.iops": "100",
"root_block_device.0.volume_id": "vol-0dcfa45e001323eac",
"root_block_device.0.volume_size": "30",
"root_block_device.0.volume_type": "gp2",
"security_groups.#": "1",
"security_groups.4272775738": "DJukes",
"source_dest_check": "true",
"subnet_id": "subnet-5b50a512",
"tags.%": "1",
"tags.Name": "DJukes-1",
"tenancy": "default",
"user_data": "fad67bb12b32d15c3f5156bf7bdd830d4e084c6f",
"volume_tags.%": "0",
"vpc_security_group_ids.#": "1",
"vpc_security_group_ids.2187737336": "sg-fd5adf87"
},
"meta": {
"e2bfb730-ecaa-11e6-8f88-34363bc7c4c0": {
"create": 600000000000,
"delete": 600000000000,
"update": 600000000000
},
"schema_version": "1"
},
"tainted": false
},
"deposed": [],
"provider": "provider.aws"
},
"aws_instance.win-example.1": {
"type": "aws_instance",
"depends_on": [
"aws_security_group.DJukes"
],
"primary": {
"id": "i-0bb714a7ddd376599",
"attributes": {
"ami": "ami-cc821eb5",
"associate_public_ip_address": "true",
"availability_zone": "eu-west-1a",
"disable_api_termination": "false",
"ebs_block_device.#": "0",
"ebs_optimized": "false",
"ephemeral_block_device.#": "0",
"iam_instance_profile": "",
"id": "i-0bb714a7ddd376599",
"instance_state": "running",
"instance_type": "t2.medium",
"ipv6_addresses.#": "0",
"key_name": "",
"monitoring": "false",
"network_interface.#": "0",
"network_interface_id": "eni-dab018e9",
"placement_group": "",
"primary_network_interface_id": "eni-dab018e9",
"private_dns": "ip-172-31-24-177.eu-west-1.compute.internal",
"private_ip": "172.31.24.177",
"public_dns": "ec2-34-249-95-222.eu-west-1.compute.amazonaws.com",
"public_ip": "34.249.95.222",
"root_block_device.#": "1",
"root_block_device.0.delete_on_termination": "true",
"root_block_device.0.iops": "100",
"root_block_device.0.volume_id": "vol-079b3491295b06249",
"root_block_device.0.volume_size": "30",
"root_block_device.0.volume_type": "gp2",
"security_groups.#": "1",
"security_groups.4272775738": "DJukes",
"source_dest_check": "true",
"subnet_id": "subnet-5b50a512",
"tags.%": "1",
"tags.Name": "DJukes-2",
"tenancy": "default",
"user_data": "fad67bb12b32d15c3f5156bf7bdd830d4e084c6f",
"volume_tags.%": "0",
"vpc_security_group_ids.#": "1",
"vpc_security_group_ids.2187737336": "sg-fd5adf87"
},
"meta": {
"e2bfb730-ecaa-11e6-8f88-34363bc7c4c0": {
"create": 600000000000,
"delete": 600000000000,
"update": 600000000000
},
"schema_version": "1"
},
"tainted": false
},
"deposed": [],
"provider": "provider.aws"
},
"aws_security_group.DJukes": {
"type": "aws_security_group",
"depends_on": [],
"primary": {
"id": "sg-fd5adf87",
"attributes": {
"description": "Managed by Terraform",
"egress.#": "1",
"egress.482069346.cidr_blocks.#": "1",
"egress.482069346.cidr_blocks.0": "0.0.0.0/0",
"egress.482069346.description": "",
"egress.482069346.from_port": "0",
"egress.482069346.ipv6_cidr_blocks.#": "0",
"egress.482069346.prefix_list_ids.#": "0",
"egress.482069346.protocol": "-1",
"egress.482069346.security_groups.#": "0",
"egress.482069346.self": "false",
"egress.482069346.to_port": "0",
"id": "sg-fd5adf87",
"ingress.#": "1",
"ingress.2601213444.cidr_blocks.#": "1",
"ingress.2601213444.cidr_blocks.0": "0.0.0.0/0",
"ingress.2601213444.description": "",
"ingress.2601213444.from_port": "0",
"ingress.2601213444.ipv6_cidr_blocks.#": "0",
"ingress.2601213444.protocol": "tcp",
"ingress.2601213444.security_groups.#": "0",
"ingress.2601213444.self": "false",
"ingress.2601213444.to_port": "6556",
"name": "DJukes",
"owner_id": "520150089049",
"revoke_rules_on_delete": "false",
"tags.%": "1",
"tags.Name": "allow-RDP",
"vpc_id": "vpc-8b23ccec"
},
"meta": {
"schema_version": "1"
},
"tainted": false
},
"deposed": [],
"provider": "provider.aws"
}
},
"depends_on": []
}
]
我想使用python将公共IP地址提取到变量
"value": [
"34.244.138.72",
"34.249.95.222"
]
到目前为止,我有以下代码:
#!/bin/python
import json
import os.path
import shutil
from os import mkdir
from pprint import pprint
from python_terraform import *
json_data=open('./terraform.json')
data = json.load(json_data)
json_data.close()
for i in range (0, len (data['modules'])):
print data['modules']['path']['outputs'][1]
并得到以下错误:
File "./1.py", line 17, in <module>
for i in range (0, len (data['modules']['path']['outputs'])):
TypeError: list indices must be integers, not str
还尝试过:
for dat in data:
print dat['outputs']
答案
我们可以从您的JSON中看到modules
是一个列表,它映射到带有数字索引的数组,而不是字典。 (虽然,列表中似乎只有一个项目。)
所以你需要用i
迭代它。
通过以下方式查看您实际收到的内容:
for i in range (0, len (data['modules'])):
pprint(data['modules'][i]['outputs']['id']['value'])
哪个输出:
$ python 1.py
[u'34.244.138.72', u'34.249.95.222']
以上是关于用python脚本搜索json文件的主要内容,如果未能解决你的问题,请参考以下文章