我有以下代码,但它会抛出错误:
import boto3
import json
import string
client = boto3.client('redshift', 'us-east-1')
cluster_list = client.describe_clusters()
result = [{"{#REDSHIFT}": row['Clusters'][0]['Endpoint']['Address']} for row in client.describe_clusters()]
result = {"data": result}
json_pre = json.dumps(result)
json_pos = json_pre.translate(None, string.whitespace)
print json_pos
但我有以下错误:
第8行,
result = [{"{#REDSHIFT}": row['Clusters'][0]['Endpoint']['Address']} for row in client.describe_clusters()]
TypeError: string indices must be integers
我想返回['Clusters'][0]['Endpoint']['Address']
的值来生成Json。
答案 0 :(得分:0)
正确的代码在这里:
import boto3
import json
import string
client = boto3.client('redshift', 'us-east-1')
cluster_list = client.describe_clusters()
result = [{"{#REDSHIFT}": red['Endpoint']['Address']} for red in cluster_list['Clusters']]
result = {"data": result}
json_pre = json.dumps(result)
json_pos = json_pre.translate(None, string.whitespace)
print json_pos