提示'NoneType' object has no attribute 'decode'错误(已解决)

在学习API项目的时候,数据可视化时出错

names, plot_dicts = [], []
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
	plot_dict = {
		'value': repo_dict['stargazers_count'],
		'label': repo_dict['description'],
}
plot_dicts.append(plot_dict)

提示’NoneType’ object has no attribute 'decode’错误

找到了解决方法:
来源链接

作者:金泽慧kathy
链接:https://www.zhihu.com/question/65438974/answer/353404292
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

去api的网站上看了下,发现s的description为null,因为缺少值所以会一直报错一个解决办法是:直接将其改为字符串

plot_dict = {
            'value': repo_dict['stargazers_count'],
            # 直接将其改为字符串
            'label': str(repo_dict['description']),
            'x_link': repo_dict['html_url']
            }

另一个解决办法是:检查repo_dict[‘description’]是否为空,若为空则填充

for repo_dict in repo_dicts: 
    names.append(repo_dict['name'])
# 检查description是否为空,若为空则填充
description = repo_dict['description']
if not description:
    description = 'No description provided.'  

plot_dict = {
        'value': repo_dict['stargazers_count'],
        'label': description,
        'x_link': repo_dict['html_url']
        }

你可能感兴趣的:(python学习)