2017-06-29 13 views
0

TypeError: 'int' object has no attribute 'getitem', 1 # rows.append([error, str(dct['value']), str(dct['uniques']),はTypeError:「int型のオブジェクトがrows.append([エラー、STR(DCT [ '値'])、STR(DCT [ 'ユニーク'])、なぜ私

わからない、何の属性 '__getitem__' がありませんエラー#の例外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 

答えて

0

次のようなものを使用:DCTから値を取得するにはdct["key"]を、しかし、「DCT」ではないようです"'int'オブジェクトに 'getitem' 'という属性がない場合は、dctの種類を確認する必要があります。 試してみてください:

print repr(dct) 

か:

print type(dct) 

をDCTの種類をチェックします。 dct["key"]を使用して値を取得することができます。値が取得されるのはdictである場合のみです。

、問題を引き起こす可能性があります別の場所は次のとおりです。dct['key']がリストされていない場合にも、DCTが辞書であればdct['key']のタイプをチェックする必要があるかもしれませんので、

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

これも、エラーが発生します。

関連する問題