json必须是str而不是字节

时间:2017-11-14 12:39:29

标签: amazon-web-services rds

def main(target_db):

# get available logs 

output = subprocess.Popen(['aws','rds', 'describe-db-log-files', '--db-instance-identifier', target_db],stdout=subprocess.PIPE).communicate()[0]

dict_output = json.loads(output)
log_file_names = map(lambda l:l['LogFileName'], dict_output['DescribeDBLogFiles'])

mkdir_p('logs/error')

# aws rds download-db-log-file-portion --db-instance-identifier prod-api --log-file-name error/postgresql.log.2015-04-20-18 --output text > logs/error/postgresql.log.2015-04-20-18
for log_file_name in log_file_names:
    content = subprocess.Popen(['aws','rds', 'download-db-log-file-portion', '--db-instance-identifier', target_db, '--log-file-name', log_file_name, '--output', 'text'],stdout=subprocess.PIPE).communicate()[0]
    with open('logs/%s' % log_file_name, 'wb') as fw:
        fw.write(content)

我收到一个错误json必须是str而不是字节在运行脚本时将它作为json对象加载(dict_output = json.loads(output))。

1 个答案:

答案 0 :(得分:0)

首先尝试打印类型:

print type(output)

如果它不是字符串,请尝试使用以下字符串将其转换为字符串:

output = str(output)dict_output = json.loads(str(output))

希望这会有所帮助。