简体   繁体   中英

Python/Pycharm: For loop fetching string instead of Dictionary

I am new to python so this may seem like a very stupid question, please bear with me.

I am using the following code: (image if debug also attached)

for connector in la_details['properties']['parameters']['$connections']['value']:
    if connector['connectionName'] in allowed_connectors:
        result = True

Pycharm 调试

Now, instead of fetching a 'dict' value in 'connector' variable, it fetches a 'string', due to which the 'if' statement gives the error:

string indices must be integers

Can someone please guide me how to get 'dict' values instead of 'string'? I tried to google but couldn't find anything that matches this scenario, maybe because I am using the wrong terminology.

EDIT: print(la_details) produces the following output:

{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'}}

In python for... in... returns either the value in the case of lists or the key in the case for dictionaries (as in your example), below is the correct way to do your loop.

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']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM