2017-06-29 49 views
0

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 

回答

0

你使用這樣的: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是一個字典。

相關問題