AttributeError:' str'对象没有属性

时间:2014-06-19 10:14:18

标签: python string list dictionary type-conversion

我是Python新手。 我有2个Dict和1个List,将按照重演方法进行处理。

 system_list= ['cassandra', 'flume', 'hbase', 'hdfs', 'mapreduce', 'zookeeper']

allaspects= {'flume': [19.0, 13, 46, 5, 100, 0, 6, 6, 0], 
 'hdfs': [161.0, 221.0, 232.5, 8.0, 60.0, 7.5, 16, 70, 10], 
 'zookeeper': [40.0, 66, 67, 13, 36, 1.5, 2, 19.5, 6], 
 'mapreduce': [161, 23, 11, 79, 41, 6.5, 8.5, 101.5, 8],
 'hbase': [270.75, 573, 264, 197, 426, 7.0, 12.5, 91, 4], 
 'cassandra': [404, 453, 287, 17, 307, 1.0, 10, 31, 26]}

allaspects_big= {'flume': [18.0, 119.0, 43.0, 3.5, 81.0, 0, 6.0, 4.5, 0], 
 'hdfs': [152.5, 214.0, 221.5, 7.0, 56.0, 6.0, 14, 68, 10], 
 'zookeeper': [33, 55, 56, 13, 28, 1.0, 1, 16.0, 3.5], 
 'mapreduce': [152, 219, 106, 71, 34, 6.5, 7.5, 91.0, 7.0],
 'hbase': [227, 505, 233, 170, 320, 6.0, 12.5, 84, 4], 
 'cassandra': [195, 271, 177, 10.5, 156, 1.0, 6, 16, 20]}



def recapitulation(system_list, total):
  count = {}
  msg =""
  # print total
  for system in system_list:
    count[system] = 0
    for data in total[system]:
      count[system] = count[system] + data
    msg= msg+""+system+"("+str(int(count[system]))+")"
    if(system == 'zookeeper'):
      msg= msg+"."
    else:
      msg= msg+", "
  return msg

我将所有脚本放在同一个文件中。 如果我只运行一个dict(在我的情况下只执行a3或仅执行a3b),则该方法为我们提供字符串输出。但是当我一起运行a3和a3b(见下文)时。我得到错误'str'没有属性。

a3 = recapitulation(system_list ,allaspects) 
a3b = recapitulation(system_list, allaspects_big) 

错误:

a3b = recapitulation(system_list, allaspects_big) 
AttributeError: 'str' object has no attribute 'allaspects_big'

完整追溯:

Traceback (most recent call last):
  File "py/parse-all.py", line 2537, in <module>
    a3b = recapitulation(system_list, a3.allaspects_big) 
AttributeError: 'str' object has no attribute 'allaspects_big'

我无意解决这个问题。请给我一些建议。我的脚本出了什么问题。谢谢!

1 个答案:

答案 0 :(得分:2)

您的代码与回溯不一致。你说你正在跑步:

a3b = recapitulation(system_list, allaspects_big) 

但追溯清楚地显示:

a3b = recapitulation(system_list, a3.allaspects_big) 
                                # ^ what?!

a3是一个字符串,由recapitulation返回。因此具有allaspects_big属性。我想您只想通过allaspects_big,因此请移除a3.以使您实际遇到的内容符合您的声明。

相关问题