列表索引必须是整数,而不是str -Python

时间:2018-08-01 19:06:43

标签: python json

我正在尝试修改此python函数以监视Zabbix中的Lambda函数 (为每个lambda函数自动创建项目/触发器)

https://github.com/omni-lchen/zabbix-cloudwatch/blob/master/awsLLD.py#L182

import re
import json
from optparse import OptionParser
from awsAccount import awsAccount
from awsConnection import awsConnection

def config_parser():
    parser = OptionParser(usage="usage: %prog [options]", version="%prog 1.0")
    parser.add_option("-a", "--account", dest="accountname", help="account name", metavar="ACCOUNT")
    parser.add_option("-r", "--region", dest="region", help="region", metavar="REGION")
    parser.add_option("-q", "--query", dest="query", help="specify a query", metavar="QUERY")
    parser.add_option("-c", "--component", dest="component", help="component name", metavar="COMPONENT")
    return parser


def get(a, r):
    account = a
    aws_account = awsAccount(account)
    aws_access_key_id = aws_account._aws_access_key_id
    aws_secret_access_key = aws_account._aws_secret_access_key
    aws_region = r
    #component = c
    # Init LLD Data
    lldlist = []
    llddata = {"data":lldlist}

    # Connect to Lambda service

    conn = awsConnection()
    conn.lambdaConnect(aws_region, aws_access_key_id, aws_secret_access_key)
    lambdaConn = conn._aws_connection

    # Save Lambda function results in a list

    functionResultsList = []
    # Save topic names in a list
    tdata = []

    # Get a list of Lambda Functions

    functionResults = lambdaConn.list_functions()
    functionResultsList.append(functionResults)
    print functionResults
    nextmarker = functionResults['Functions']['NextMarker']


if __name__ == '__main__':
    parser = config_parser()
    (options, args) = parser.parse_args()

    account = options.accountname
    region = options.region
    query = options.query


get(account,region)

print functionResults给出这些结果(2个函数)

{u'Functions': [{u'Description': u'',
                 u'LastModified': u'2018-08-01T18:50:04.214+0000',
                 u'ConfigurationId': u'e97b805a-c947-4c56-9a2e-5bef3c4cc6c5',
                 u'CodeSize': 222,
                 u'FunctionARN': u'arn:aws:lambda:eu-west-1:233135199200:function:test',
                 u'MemorySize': 128,
                 u'Handler': u'lambda_function.lambda_handler',
                 u'Role': u'arn:aws:iam::233135199200:role/lambda_basic_execution',
                 u'Mode': u'event',
                 u'Timeout': 3, u'Runtime': u'python2.7',
                 u'FunctionName': u'test'},
                {u'Description': u'',
                 u'LastModified': u'2018-06-18T12:17:34.362+0000',
                 u'ConfigurationId': u'b3c59ce0-f028-43b2-8c34-a73d2bb41782',
                 u'CodeSize': 1436,
                 u'FunctionARN': u'arn:aws:lambda:eu-west-1:233135199200:function:email',
                 u'MemorySize': 128,
                 u'Handler': u'lambda_function.lambda_handler',
                 u'Role': u'arn:aws:iam::233135199200:role/lambda_basic_execution',
                 u'Mode': u'event',
                 u'Timeout': 183,
                 u'Runtime': u'python2.7',
                 u'FunctionName': u'email'}],
               u'NextMarker': None}

下一行为我带来了一个问题:

从当前结果中获取下一个标记,该标记将用于获取下一个标记

nextmarker = functionResults ['Functions'] ['NextMarker']

File "./sns.py", line 51, in get
    nextmarker = functionResults['Functions']['Description']['NextMarker']
TypeError: list indices must be integers, not str

所需结果应为:无

functionResults输出变量中的最后一个单词

2 个答案:

答案 0 :(得分:2)

问题在于functionResults['Functions']是一个列表,而不是字典,因此您需要传递整数索引。但是'NextMarker'键看起来也位于functionResults内。 / p>

我认为您想要的是:

nextmarker = functionResults['NextMarker']

答案 1 :(得分:1)

您的值很清楚:functionResults["Functions"]是一个很大的列表。您不能使用字符串为它建立索引。也许functionResults["Functions"][0],或者从值中删除列表容器。

{u'Functions': [      # <=== here's the problem !
    {u'Description': u'',
     u'LastModified': u'2018-08-01T18:50:04.214+0000',
     u'ConfigurationId': u'e97b805a-c947-4c56-9a2e-5bef3c4cc6c5', 
     ...}],
 u'NextMarker': None}

但是请注意,NextMarker项中的Functions不是 ;它本身就是一个条目。这应该可以满足您的期望。

nextmarker = functionResults['NextMarker']