我是python的新手,所以这似乎是一个非常愚蠢的问题,请多多包涵。
我正在使用以下代码:(如果还附加了调试,则为图片)
for connector in la_details['properties']['parameters']['$connections']['value']:
if connector['connectionName'] in allowed_connectors:
result = True
现在,它不是获取'connector'变量中的'dict'值,而是获取了'string',因此'if'语句给出了错误:
string indices must be integers
[有人可以指导我如何获取'dict'值而不是'string'吗?我试图用Google搜索,但找不到与这种情况匹配的任何内容,可能是因为我使用了错误的术语。
编辑:print(la_details)产生以下输出:
{u'name': u'REDACTED',
u'tags': {},
u'id': u'/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Logic/workflows/REDACTED',
u'location': u'REDACTED',
u'type': u'Microsoft.Logic/workflows',
u'properties': {u'definition': {u'parameters': {u'$connections': {u'defaultValue': {}, u'type': u'Object'}}, u'triggers': {u'Recurrence': {u'recurrence': {u'frequency': u'Month', u'interval': 1}, u'type': u'Recurrence'}}, u'outputs': {}, u'actions': {u'Send_an_email_(V2)': {u'inputs': {u'body': {u'Body': u'<p>TEST</p>', u'To': u'REDACTED', u'Subject': u'TEST'}, u'path': u'/v2/Mail', u'host': {u'connection': {u'name': u"@parameters('$connections')['office365']['connectionId']"}}, u'method': u'post'}, u'runAfter': {}, u'type': u'ApiConnection'}, u'Get_tables_(V2)': {u'inputs': {u'path': u"/v2/datasets/@{encodeURIComponent(encodeURIComponent('default'))},@{encodeURIComponent(encodeURIComponent('default'))}/tables", u'host': {u'connection': {u'name': u"@parameters('$connections')['sql']['connectionId']"}}, u'method': u'get'}, u'runAfter': {u'Send_an_email_(V2)': [u'Succeeded']}, u'type': u'ApiConnection'}}, u'contentVersion': u'1.0.0.0', u'$schema': u'https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#'}, u'version': u'REDACTED', u'parameters': {u'$connections': {u'value': {u'office365': {u'connectionId': u'/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Web/connections/office365', u'id': u'/subscriptions/REDACTED/providers/Microsoft.Web/locations/eastus/managedApis/office365', u'connectionName': u'office365'}, u'sql': {u'connectionId': u'/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Web/connections/sql', u'id': u'/subscriptions/REDACTED/providers/Microsoft.Web/locations/eastus/managedApis/sql', u'connectionName': u'sql'}}}}, u'integrationServiceEnvironment': {u'type': u'Microsoft.Logic/integrationServiceEnvironments', u'name': u'ise-demo-res', u'id': u'/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Logic/integrationServiceEnvironments/ise-demo-res'}, u'endpointsConfiguration': {u'connector': {u'outgoingIpAddresses': [{u'address': u'40.71.11.80/28'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}]}, u'workflow': {u'outgoingIpAddresses': [{u'address': u'REDACTED'}, {u'address': u'REDACTED'}], u'accessEndpointIpAddresses': [{u'address': u'REDACTED'}]}}, u'state': u'Enabled', u'accessEndpoint': u'REDACTED', u'createdTime': u'2020-03-12T14:45:34.6908438Z', u'changedTime': u'2020-04-08T10:35:32.3388762Z', u'provisioningState': u'Succeeded'}}
在python中,for ... in ...
在数组的情况下返回值,在字典的情况下返回属性名(如您的示例),以下是执行循环的正确方法。
la_details = {
'properties': {
'parameters': {
'$connections': {
'value': {
'office365': { 'connectionName': 'office365' },
'sql': { 'connectionName': 'sql' },
}
}
}
}
}
allowed_connectors = ['sql']
#for connector in la_details['properties']['parameters']['$connections']['value']:
# if connector['connectionName'] in allowed_connectors:
# return True
# else:
# return 'False
values = la_details['properties']['parameters']['$connections']['value']
for connector in values:
if values[connector]['connectionName'] in allowed_connectors:
print('{0} is allowed'.format(values[connector]['connectionName']))
else:
print('{0} is not allowed'.format(values[connector]['connectionName']))