Python版本2.7.10
我有这个脚本(抓取AWS EBS卷元数据),该脚本当前生成key=value
对数据,每行输出CSV(逗号分隔值)。
Python脚本:
#!/usr/bin/python
#Do `sudo pip install boto3` first
import boto3
import json
def generate(key, value):
"""
Creates a nicely formatted Key(Value) item for output
"""
return '{}={}'.format(key, value)
#if isinstance(value,int):
# return '\"{}\": {}'.format(key, value)
#else:
# return '\"{}\": \"{}\"'.format(key, value)
def main():
ec2 = boto3.resource('ec2', region_name="us-west-2")
volumes = ec2.volumes.all()
for vol in volumes:
if vol.state == "available":
vol_state_num_value = 1
else:
vol_state_num_value = 0
if vol.snapshot_id == "":
vol_snapshot_id = "None"
else:
vol_snapshot_id = vol.snapshot_id
output_parts = [
# Volume level details
generate('vol_id', vol.volume_id),
generate('az', vol.availability_zone),
generate('vol_type', vol.volume_type),
generate('size', vol.size),
generate('iops', vol.iops),
generate('snapshot_id', vol_snapshot_id),
generate('vol_state', vol_state_num_value),
]
# only process when there are tags to process
if vol.tags:
for _ in vol.tags:
# Get all of the tags
output_parts.extend([
generate(_.get('Key'), _.get('Value')),
])
# At last put volume state numberic value
# i.e. 0 (in-use) and 1 (available/unattached) volume
output_parts.extend([
generate('state', vol_state_num_value),
])
# output everything at once.
print ','.join(output_parts)
#print '{}{}{}'.format('{',output_parts,'}')
if __name__ == '__main__':
main()
目前,它生成的输出如下所示:
vol_id=vol-0abcdab1b68111f8b,az=us-west-2b,vol_type=gp2,size=5,iops=100,snapshot_id=snap-0abcdab1b68111f8b,vol_state=0,mirror=primary,autoscale=true,cluster=customer,Name=[customer-2b-app41] primary,role=app,hostname=customer-2b-app41-i-0abcdab1b68111f8b,state=0
vol_id=vol-0abcdab1b68111f8c,az=us-west-2b,vol_type=gp2,size=12,iops=100,snapshot_id=snap-0abcdab1b68111f9c,vol_state=0,state=0
我尝试转换脚本,以便不是每行生成key=value
对CSV行,而是生成一个JSON对象。
我尝试使用IF语句调整脚本,如脚本中所示isinstance()
表示值部分(用双引号包装值或忽略它,如果它的数字和使用以下内容) line:
#if isinstance(value,int):
# return '\"{}\": {}'.format(key, value)
#else:
# return '\"{}\": \"{}\"'.format(key, value)
和
#print '{}{}{}'.format('{',output_parts,'}')
但是,那并没有给我预期的结果。
期望得到的结果如下:
{
{
"vol_id": "vol-0abcdab1b68111f8b",
"az": "us-west-2b",
"vol_type": "gp2",
"size": 5,
"iops": 100,
"snapshot_id":"snap-0abcdab1b68111f8b",
...,
.....,
},
{
"vol_id": "vol-0abcdab1b68111f8c",
"az": "us-west-2b",
"vol_type": "gp2",
"size": 12,
"iops": 100,
"snapshot_id": "snap-0abcdab1b68111f9c",
...,
.....
}
}
我尝试使用json.dumps(output_parts)
,但这对获得所需的输出没有帮助。我不需要CSV输出。
答案 0 :(得分:1)
要将csv
行转换为dict
,请执行以下操作:
<强>代码:强>
def my_csv_to_dict(csv_line):
return dict(csv.split('=') for csv in csv_line.split(','))
测试代码:
要将这些词典转换为json,您可以使用json
lib。
test_data = [x.strip() for x in """
vol_id=vol-0abcdab1b68111f8b,az=us-west-2b,vol_type=gp2,size=5,iops=100,snapshot_id=snap-0abcdab1b68111f8b,vol_state=0,mirror=primary,autoscale=true,cluster=customer,Name=[customer-2b-app41] primary,role=app,hostname=customer-2b-app41-i-0abcdab1b68111f8b,state=0
vol_id=vol-0abcdab1b68111f8c,az=us-west-2b,vol_type=gp2,size=12,iops=100,snapshot_id=snap-0abcdab1b68111f9c,vol_state=0,state=0
""".split('\n')[1:-1]]
import json
print(json.dumps([my_csv_to_dict(x) for x in test_data], indent=2))
<强>结果:强>
[
{
"az": "us-west-2b",
"autoscale": "true",
"Name": "[customer-2b-app41] primary",
"mirror": "primary",
"cluster": "customer",
"state": "0",
"iops": "100",
"role": "app",
"vol_type": "gp2",
"snapshot_id": "snap-0abcdab1b68111f8b",
"vol_id": "vol-0abcdab1b68111f8b",
"vol_state": "0",
"hostname": "customer-2b-app41-i-0abcdab1b68111f8b",
"size": "5"
},
{
"az": "us-west-2b",
"state": "0",
"iops": "100",
"vol_type": "gp2",
"snapshot_id": "snap-0abcdab1b68111f9c",
"vol_id": "vol-0abcdab1b68111f8c",
"vol_state": "0",
"size": "12"
}
]