Python对象迭代

Python对象迭代,python,Python,是否有人可以提供一个示例,说明如何在python中循环这个对象,并在api='interest'和arguments.name='FileName'中提取'value' 这是我到目前为止的资料。 此对象有更多的进程和调用…输出已被忽略 编辑:我应该指出,在运行此代码时,我遇到以下错误: TypeError:列表索引必须是整数,而不是str 你现在做的似乎还可以, 但是 您的索引已关闭(请仔细查看列表 您的检查似乎无效(v是一个字符串,因此v['api']无效) 因此,请尝试这样做,(我将您的对象

是否有人可以提供一个示例,说明如何在python中循环这个对象,并在
api='interest'
arguments.name='FileName'
中提取'value'

这是我到目前为止的资料。
此对象有更多的进程和调用…输出已被忽略

编辑:我应该指出,在运行此代码时,我遇到以下错误: TypeError:列表索引必须是整数,而不是str


你现在做的似乎还可以, 但是

  • 您的索引已关闭(请仔细查看列表
  • 您的检查似乎无效(
    v
    是一个字符串,因此
    v['api']
    无效)
  • 因此,请尝试这样做,(我将您的对象设置为
    I

    或者,如果列表中有多个部分

    for proc in i['behavior']['processes']:
        for call in proc['calls']:
            print 'api =>',call['api'] # a if here
            for args in call['arguments']:
                print '   argument.name =>',args['name'] # and another if here should do the trick.
    
    为什么会出现错误
    试试下面的代码,你就会明白你做错了什么

    print type(i['behavior']['processes'])
    print type(i['behavior']['processes'][0])
    print type(i['behavior']['processes'][0]['calls'])
    print type(i['behavior']['processes'][0]['calls'][0])
    

    是一个列表,而不是命令。

    您在问题中作为初学者给出的内容将不起作用,因为您没有遍历列表中的元素,这些元素分别是键
    “进程”
    “调用”
    的值。也就是说,您将需要类似的内容

    for proc in object ['processes']:
        for call in proc ['calls']:
            if call ['api'] == "interesting":
                fname = None
                for arg in call ['arguments']:
                    if arg ['name'] == "FileName":
                        fname = arg ['value']
    

    然后,您要查找的文件名将位于
    fname
    中。这没有错误检查,因为我不知道您的数据来自何处。

    继续您开始的工作有什么问题吗?提供输入的每个人都是正确的,但最后我使用了此代码。非常感谢大家。
    for dct in i['behavior']['processes'][0]['calls']:
        if dct['api'] == "interesting":
            print 'api',dct['api']
    
    for dct in i['behavior']['processes'][0]['calls']:
        for k,v in dct.items():
            if k == 'api' and  v =="interesting":
                print 'api',dct['api']
    
    for proc in i['behavior']['processes']:
        for call in proc['calls']:
            print 'api =>',call['api'] # a if here
            for args in call['arguments']:
                print '   argument.name =>',args['name'] # and another if here should do the trick.
    
    print type(i['behavior']['processes'])
    print type(i['behavior']['processes'][0])
    print type(i['behavior']['processes'][0]['calls'])
    print type(i['behavior']['processes'][0]['calls'][0])
    
    object['behavior']['processes']['calls']
    
    for proc in object ['processes']:
        for call in proc ['calls']:
            if call ['api'] == "interesting":
                fname = None
                for arg in call ['arguments']:
                    if arg ['name'] == "FileName":
                        fname = arg ['value']