【发布时间】:2021-08-27 20:50:07
【问题描述】:
我正在尝试使用 Python 从 GCP Recommendations API 的 API JSON 输出中非法获取某些键值,并且我对使用 Python 比较陌生。我试图非法获取的大多数值都可以毫无问题地获取,但是,当我尝试在 JSON 中更深层嵌套的代码块中非法获取某些值时,它会失败并出现错误:TypeError: 'OperationGroup' object is not subscriptable
JSON 响应的完整输出在这里(更改了一些值以保护公司信息):
name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcd-efg-hijk-lmnop-qrstuv-123456"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
seconds: 1623222401
}
primary_impact {
category: COST
cost_projection {
cost {
currency_code: "USD"
units: -12
nanos: -98539964
}
duration {
seconds: 2592000
}
}
}
content {
operation_groups {
operations {
action: "test"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/abcname123"
path: "/machineType"
value_matcher {
matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
}
}
operations {
action: "replace"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/abcname123"
path: "/machineType"
value {
string_value: "zones/us-central1-a/machineTypes/e2-small"
}
}
}
}
state_info {
state: ACTIVE
}
etag: "\"abc-123-def-456\""
recommender_subtype: "CHANGE_MACHINE_TYPE"
name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcdefg-hijklmnop-123-456"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
seconds: 1623222401
}
primary_impact {
category: COST
cost_projection {
cost {
currency_code: "USD"
units: -12
nanos: -99648292
}
duration {
seconds: 2592000
}
}
}
content {
operation_groups {
operations {
action: "test"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/instance-example1"
path: "/machineType"
value_matcher {
matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
}
}
operations {
action: "replace"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/instance-example1"
path: "/machineType"
value {
string_value: "zones/us-central1-a/machineTypes/e2-small"
}
}
}
}
state_info {
state: ACTIVE
}
etag: "\"abcdefg12345\""
recommender_subtype: "CHANGE_MACHINE_TYPE"
name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcd1234"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
seconds: 1623222401
}
primary_impact {
category: COST
cost_projection {
cost {
currency_code: "USD"
units: -11
nanos: -568971875
}
duration {
seconds: 2592000
}
}
}
content {
operation_groups {
operations {
action: "test"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
path: "/machineType"
value_matcher {
matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
}
}
operations {
action: "replace"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
path: "/machineType"
value {
string_value: "zones/us-central1-a/machineTypes/e2-small"
}
}
}
}
state_info {
state: ACTIVE
}
etag: "\"abcd1234\""
recommender_subtype: "CHANGE_MACHINE_TYPE"
这是我的 Python 代码:
from google.cloud import recommender
import os
client = recommender.RecommenderClient()
def main():
name = client.list_recommendations(parent='projects/xyzproject/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender')
for element in name:
# print(element)
print(element.description)
print(element.primary_impact.category)
print(element.primary_impact.cost_projection.cost.currency_code)
print(element.primary_impact.cost_projection.cost.units)
print(element.state_info.state)
print(element.content.operation_groups)
for item in element.content.operation_groups:
print(item['resource_type'])
main()
以上工作的以下部分:
print(element.description)
print(element.primary_impact.category)
print(element.primary_impact.cost_projection.cost.currency_code)
print(element.primary_impact.cost_projection.cost.units)
print(element.state_info.state)
print(element.content.operation_groups)
但我遇到的错误和麻烦是:
for item in element.content.operation_groups:
print(item['resource_type'])
每当我尝试使用 python 脚本的那部分时,它都会失败并出现错误:
TypeError: 'OperationGroup' object is not subscriptable
那么,有人可以帮助我了解如何正确利用 JSON 响应并非法获取以下块中的信息(例如“resource_type”)吗?
content {
operation_groups {
operations {
action: "test"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
path: "/machineType"
value_matcher {
matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
}
}
operations {
action: "replace"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
path: "/machineType"
value {
string_value: "zones/us-central1-a/machineTypes/e2-small"
}
}
}
}
【问题讨论】:
-
响应似乎不是 JSON 或标准字典,因为您使用的是 '.'访问它的方法,而不是键。无论 OperationGroup 是什么对象,它都是不可下标的,因此您无法对其进行迭代。您需要先了解对象类型,或者找到一种方法将其全部转换为
dict。 -
您很可能在 Python 中转储对象,而不是将对象转换为 JSON 字符串以进行输出。在 Google 返回的 JSON 中,
operationGroups是一个操作数组,它是另一个数组。伪数据模型:content.operationGroups[].operations[]. -
@match - 我如何首先将这一切转换为字典?
-
@JohnHanley- 您是否建议尝试在代码中编写 content.operation_groups[].operations[] 以便在操作中访问数组?
-
所以经过一些研究,响应是一种称为 JSON-PATCH 的 JSON。即使我将内容更改为以这种方式打印,我仍然会遇到相同的错误:
print(element.content.operation_groups[0]['resource_type'])——这会导致TypeError: 'OperationGroup' object is not subscriptable。有人可以帮忙吗?
标签: python-3.x for-loop google-cloud-platform nested-loops google-cloud-recommendation