use*_*567 23 python reportlab ptvs
首先,我是Python新手.我正在使用PTVS http://pytools.codeplex.com/.接下来我安装了reportlab.然后我在https://github.com/nakagami/reportlab/blob/master/demos/colors/colortest.py#L68上运行一个示例演示但是在线,
all_colors = reportlab.lib.colors.getAllNamedColors().items()
all_colors.sort() # alpha order by name
Run Code Online (Sandbox Code Playgroud)
我收到了错误, dict_items object has no attribute sort
Joh*_*han 45
没有测试但是理论:你正在使用python3!
来自https://docs.python.org/3/whatsnew/3.0.html
dict方法dict.keys(),dict.items()和dict.values()返回"views"而不是list.例如,这不再有效:k = d.keys(); k.sort().使用k = sorted(d)代替(这也适用于Python 2.5,效率也很高).
据我所知,"视图"是一个迭代器,而迭代器没有sort函数.将其更改为
sorted(all_colors)
Run Code Online (Sandbox Code Playgroud)
根据文件
因此,根据约翰的答案,总的解决方案是:
all_colors = sorted(reportlab.lib.colors.getAllNamedColors().items())
Run Code Online (Sandbox Code Playgroud)