类型错误: 'INT' 对象没有属性 '__getitem__',rows.append([出错,STR(DCT [ '值']),STR(DCT [ '不重复']),

问题描述:

TypeError: 'int' object has no attribute 'getitem', 1 # rows.append([error, str(dct['value']), str(dct['uniques']),类型错误: 'INT' 对象没有属性 '__getitem__',rows.append([出错,STR(DCT [ '值']),STR(DCT [ '不重复']),

不知道为什么我正在错误#类型错误:“诠释”对象有没有属性“的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 

你使用这样的:dct["key"]摆脱DCT值,但它似乎“DCT”不一个字典根据错误信息:“'int'对象没有属性'getitem'”,你可能需要检查dct的类型 尝试:

print repr(dct) 

或:

print type(dct) 

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

,另一个可能引起问题的地方是:

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

,这也将提高一个错误,如果dct['key']不是一个列表,所以您可能还需要检查的dct['key']类型,如果DCT是一个字典。