从另一个lis中的值创建包含JSON数组的列表

2024-03-28 09:51:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,它发送一个JSON post。部分有效负载来自以下格式的列表

SERVICES = [{'id':'PZV8CL7', 'type':'service'}, {'id':'PYMOSPH', 'type':'service'}]

payload = {
    'maintenance_window': {
        'services': SERVICES,
    }

当手动将服务添加到脚本中时,脚本工作得很好,但是我需要它能够处理很多服务,因此我添加了以下内容:

code = ['PH6FKI0', 'PD1EK3Z', 'PSJR02A', 'PI8VRN1']
list = []

for c in code:
    list.append("{'id':'%s', 'type':'service'}" % s) 

print(list)
["{'id':'PH6FKI0', 'type':'service'}", "{'id':'PD1EK3Z', 'type':'service'}"

这里的问题是,在返回错误的JSON post中发送时,引号似乎会产生干扰

['Services must be an object containing properties id and type.']

我怎样才能在不加引号的情况下列一份清单呢?任何其他的方法都会很棒

提前谢谢


Tags: 脚本idjson列表格式typeservicecode