我有这个脚本,假设根据数字对水果进行排名。
#! /usr/bin/python
fruits = {'Apple':1,'Banana':2,'Pineapple':3}
alist = [(v, k) for k, v in fruits.items()]
alist.sort(reverse=False)
fruit_order = str(alist).translate(None, "[]()'123456789").replace(", , ", " , ",").replace(" ", "")[1:]
print fruit_order
水果实际上是一个用 .append(fruits) 检查组成的列表
#! /usr/bin/python
import re
from configobj import ConfigObj
config = ConfigObj('sites.conf')
fruits = []
substance = "juice"
target_list = config['MainConfig']['fruits']
for target in target_list:
if re.search( config[target]['Skiplist'], substance, re.M|re.I):
target = target + "'" + ":" + config[target]['Rank'] + ","
fruits.append(target)
else:
print "Dident pass"
fruits1 = "{'" + "'".join(fruits) + "}"
alist = [(v, k) for k, v in fruits1.list()]
alist.sort(reverse=False)
fruit_rank = str(alist).translate(None, "[]()'123456789").replace(", , ", ",").replace(" ", "")[1:]
示例 2 中的 print fruit_rank 打印出与示例 1 中的 test 完全相同的行。
但是我仍然在 example2 上遇到错误,AttributeError: 'str' object has no attribute 'list'
我无法真正弄清楚这一点。我该怎么做?
我正在寻找的是在配置文件中我对每个水果都有一个等级,该行应该打印出它的等级中的水果。