获取以下字典和键值对列表:
[{'name': 'test-project',
'properties': {'name': 'test-project',
'parent': {'id': '', 'type': 'folder'},
'projectId': 'test-project'},
'type': 'cloudresourcemanager.v1.project'},
{'metadata': {'dependsOn': ['test-project']},
'name': 'billing_test-project',
'properties': {'billingAccountName': 'billingAccountName',
'name': 'projects/test-project'},
'type': 'deploymentmanager.v2.virtual.projectBillingInfo'},
{'name': 'apis',
'properties': {'apis': ['compute.googleapis.com'],
'billing': 'billing_test-project',
'concurrent_api_activation': True,
'project': 'test-project'},
'type': 'apis.py'},
{'name': 'service-accounts',
'properties': {'project': 'test-project',
'service-accounts': ''},
'type': 'service-accounts.py'},
{'action': 'gcp-types/compute-v1:compute.projects.setUsageExportBucket',
'metadata': {'dependsOn': ['test-project',
'test-project-compute.googleapis.com']},
'name': 'set-export-bucket',
'properties': {'bucketName': 'gs://usage-exports',
'project': 'test-project',
'reportNamePrefix': 'usage_gce_'}}]}
我需要将其转换为以下语法:
资源:\ n-名称:test-project \ n属性:\ n名称:test-project \ n父级:\ n id:\ n类型:文件夹\ n
我认为在简短的Google搜索后,以下内容可能会起作用:
'\n'.join(d for d in resources)
不幸的是,这给了我错误: “ TypeError:序列项0:预期的str实例,找到了dict”
任何帮助,将不胜感激。
(作为一个附带说明,Google唯一的配置内容为字符串的示例是用于创建VM:https://cloud.google.com/deployment-manager/docs/deployments#api;我注意到每个键值对的空间似乎都在增加,但是我并不是这样完全确定是否确实需要这样做)。
编辑: 抱歉,我的意思是说我需要使用类似于以下格式的键值对作为字符串:
resource = "resources:\n- name: vm-created-by-cloud-config\n type: compute.v1.instance\n properties:\n zone: us-central1-a\n machineType: https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/machineTypes/n1-standard-1\n disks:\n - deviceName: boot\n type: PERSISTENT\n boot: true\n autoDelete: true\n initializeParams:\n diskName: disk-created-by-cloud-config\n sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20151104\n networkInterfaces:\n - network: https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default\n"
因此,在打印时,它看起来像这样:
resources:
- name: test-project
properties:
name: test-project
parent:
id:
type: folder
projectId: test-project
type: cloudresourcemanager.v1.project
- metadata:
dependsOn: test-project
name: billing_test-project
properties:
billingAccountName: billingAccountName
name: projects/test-project
type: deploymentmanager.v2.virtual.projectBillingInfo
- name: apis
properties:
apis: compute.googleapis.com
billing: billing_test-project
concurrent_api_activation: True
project: test-project
type: apis.py
- name: service-accounts
properties:
project: test-project
service-accounts:
type: service-accounts.py
- action: gcp-types/compute-v1:compute.projects.setUsageExportBucket
metadata:
dependsOn: test-project,test-project-compute.googleapis.com
name: set-export-bucket
properties:
bucketName: gs://usage-exports
project: test-project
reportNamePrefix: usage_gce_
答案 0 :(得分:3)
尝试应用以下递归函数。这应该适合您的特定用例:
resources = {'resources': [{'name': 'test-project',
'properties': {'name': 'test-project',
'parent': {'id': '', 'type': 'folder'},
'projectId': 'test-project'},
'type': 'cloudresourcemanager.v1.project'},
{'metadata': {'dependsOn': ['test-project']},
'name': 'billing_test-project',
'properties': {'billingAccountName': 'billingAccountName',
'name': 'projects/test-project'},
'type': 'deploymentmanager.v2.virtual.projectBillingInfo'},
{'name': 'apis',
'properties': {'apis': ['compute.googleapis.com'],
'billing': 'billing_test-project',
'concurrent_api_activation': True,
'project': 'test-project'},
'type': 'apis.py'},
{'name': 'service-accounts',
'properties': {'project': 'test-project',
'service-accounts': ''},
'type': 'service-accounts.py'},
{'action': 'gcp-types/compute-v1:compute.projects.setUsageExportBucket',
'metadata': {'dependsOn': ['test-project',
'test-project-compute.googleapis.com']},
'name': 'set-export-bucket',
'properties': {'bucketName': 'gs://usage-exports',
'project': 'test-project',
'reportNamePrefix': 'usage_gce_'}}]}
def unpack_dict(d, spaces=0):
try:
s = ' ' * spaces
spaces += 2
return ' '.join([f'\n{s}{k}: {unpack_dict(v, spaces)}' for k, v in d.items()])
except AttributeError:
if isinstance(d, list):
return ''.join([unpack_dict(item) for item in d])
else:
return d
result = unpack_dict(resources).strip()
print(result)
的输出
resources:
name: test-project
properties:
name: test-project
parent:
id:
type: folder
projectId: test-project
type: cloudresourcemanager.v1.project
metadata:
dependsOn: test-project
name: billing_test-project
properties:
billingAccountName: billingAccountName
name: projects/test-project
type: deploymentmanager.v2.virtual.projectBillingInfo
name: apis
properties:
apis: compute.googleapis.com
billing: billing_test-project
concurrent_api_activation: True
project: test-project
type: apis.py
name: service-accounts
properties:
project: test-project
service-accounts:
type: service-accounts.py
action: gcp-types/compute-v1:compute.projects.setUsageExportBucket
metadata:
dependsOn: test-projecttest-project-compute.googleapis.com
name: set-export-bucket
properties:
bucketName: gs://usage-exports
project: test-project
reportNamePrefix: usage_gce_
请注意:
format()
类的str
函数。unpack_dict()
函数,传递该时间字典而不是原始的resources
。