names, plot_dicts = [], []
for repo_dict in repo_dics:
names.append(repo_dict['name'])
plot_dict = {
'value':repo_dict['stargazers_count'],
'label':repo_dict['description']
}
用Pygal绘制条形图时报错:
File "D:\Program Files (x86)\PycharmProject\Study\venv\lib\site-packages\pygal\graph\bar.py", line 116, in bar
metadata)
File "D:\Program Files (x86)\PycharmProject\Study\venv\lib\site-packages\pygal\util.py", line 233, in decorate
metadata['label'])
File "D:\Program Files (x86)\PycharmProject\Study\venv\lib\site-packages\pygal\_compat.py", line 61, in to_unicode
return string.decode('utf-8')
***AttributeError: 'NoneType' object has no attribute 'decode'***
错误原因:
'label':repo_dict['description']
有一个repo_dict[‘description’]为空。
如何发现这个错误的?
根据控制台打印,打开报错代码:
if 'label' in metadata:
svg.node(node, 'desc', class_='label').text = to_unicode(
metadata['label'])
return node
def to_unicode(string):
"""Force string to be a string in python 3 or a unicode in python 2"""
if not isinstance(string, coerce):
return string.decode('utf-8')
return string
发现对‘label’的值有一个解码,如果value为空,那么肯定无法解码,所以报:NoneType’ object has no attribute 'decode’的错误。
解决方法:
'label':str(repo_dict['description'])
一起学习,一起进步,加油!