TypeError:'int'对象没有属性'__getitem__',rows.append([error,str(dct ['value']),str(dct ['uniques']),

时间:2017-06-29 09:33:26

标签: python-2.7

  

TypeError:'int'对象没有属性' getitem ',1#     rows.append([error,str(dct ['value']),str(dct ['uniques']),

不确定我为什么会收到错误#TypeError:'int'对象没有属性' getitem ',1#

def errors_to_csv(errors):
    rows = [HEADER]
    for error, dct in errors.items():
            rows.append([error, str(dct['value']), str(dct['uniques']),
                         str(dct['percentage_total']),
                         str(dct['percentage_runs']), str(dct['links'][:10]),
                         str(dct['additional_info'][:11])])
            if error == 'runs':
                continue
    return rows

def capture_data_in_json(product, recipients, runtype, startdate, enddate):
        errors = dd(lambda: {'value': 0, 'uniques': 0, 'percentage_total': None,
                             'percentage_runs': None, 'links': [],
                             'additional_info': []})
        last_runs = get_last_n_runs(product, RESULTS_LIMIT, runtype, RUN_STATUS, startdate, enddate)
        #last_runs += get_last_n_runs(product, RESULTS_LIMIT, 'distributed-test',
                                     #RUN_STATUS, startdate, enddate)
        log.info('Collected {0} runs'.format(len(last_runs)))
        outputs = [get_file_from_uuid('console.log', run['uuid']) for run in last_runs]
        log.info('Outputs collected')
        for output, link in outputs:
            current = dd(lambda: 0)
            lines = output.split('\n')
            for index, line in enumerate(lines):
                line = ''.join([i for i in line if not i.isdigit()])
                if '[ERROR]' in line:
                    errors[line]['value'] += 1
                    errors[line]['links'] += [link]
                    errors[line]['additional_info'] += lines[index - 5: index + 5]
                    if line not in current.keys():
                        current[line] += 1

            for line in current.keys():
                errors[line]['uniques'] += 1

        total_errors = sum([errors[error]['value'] for error in errors.keys()])
        for error in errors.keys():
            errors[error]['percentage_total'] = round(100.0 * errors[error]['value'] / total_errors, 2)
            errors[error]['percentage_runs'] = round(100.0 * errors[error]['uniques'] / len(last_runs), 2)

        log.info('Analyzed {0} runs.'.format(len(outputs)))
        errors['runs'] = len(outputs)
        with open('data_' + product + '.json', 'w+') as out_file:
            json.dump(errors, out_file)
        return errors

1 个答案:

答案 0 :(得分:0)

您使用类似:dct["key"]的内容来获取dct的值,但根据错误消息,似乎“dct”不是dict:“'int'对象没有属性' getitem < / strong>'“,您可能需要检查dct的类型。 尝试:

print repr(dct)

或:

print type(dct)

检查dct的类型。 你可以使用:dct["key"]获取一个值,只有当它是一个字典。

另一个可能导致问题的地方是:

str(dct['key'][:end])

如果dct['key']不是列表,这也会引发错误,因此如果dct是dict,您可能还需要检查dct['key']的类型。