python - chart.render_to_file('python_repos.svg')AttributeError: 'NoneType' 对象没有属性 'decode'
问题描述
我正在通过这本书学习 python:“Python Crash Course: A Hands-On, Project-Based Introduction to Programming”。在“使用 API”一书的第 17 章中,我按照书中所说的做了,但是发生了错误。我不知道错误是什么意思。这是否意味着站点包有问题?如何解决?
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
# Make an API call, and store the response.
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code:", r.status_code)
# Store API response in a variable.
response_dict = r.json()
print("Total repositories:", response_dict['total_count'])
# Explore information about the repositories.
repo_dicts = response_dict['items']
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)
# Make visualization.
my_style = LS('#333366', base_style=LCS)
my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
my_config.truncate_label = 15
my_config.show_y_guides = False
my_config.width = 1000
chart = pygal.Bar(my_config, style=my_style)
chart.title = 'Most-Starred Python Projects on GitHub'
chart.x_labels = names
chart.add('', plot_dicts)
chart.render_to_file('python_repos.svg')
错误如下
Status code: 200
Total repositories: 2198954
Traceback (most recent call last):
File "2018-07-13-1.py", line 45, in <module>
chart.render_to_file('python_repos.svg')
File "C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\pygal\graph\public.py", line 114, in render_to_file
f.write(self.render(is_unicode=True, **kwargs))
File "C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\pygal\graph\public.py", line 52, in render
self.setup(**kwargs)
File "C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\pygal\graph\base.py", line 217, in setup
self._draw()
File "C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\pygal\graph\graph.py", line 933, in _draw
self._plot()
File "C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\pygal\graph\bar.py", line 146, in _plot
self.bar(serie)
File "C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\pygal\graph\bar.py", line 116, in bar
metadata)
File "C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\pygal\util.py", line 233, in decorate
metadata['label'])
File "C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\pygal\_compat.py", line 61, in to_unicode
return string.decode('utf-8')
AttributeError: 'NoneType' object has no attribute 'decode'
解决方案
该错误是由您在图表上设置的标签之一引起的None
,它是Python 的特殊内置常量之一。当 pygal 来渲染图表时,它期望标签是一个字符串并调用该decode
函数。对象上不存在此函数None
,因此会引发错误。
查看 Github 存储库的名称和描述(您分别设置为 x 标签和数据标签),我们可以看到其中一个存储库的描述是None
. 我不会说书中的代码有什么特别的错误,只是运气不好,目前最受关注的 Python 存储库之一没有描述。
None
我们可以通过检查for 循环中的值来解决这个问题:
for repo_dict in repo_dicts:
if repo_dict['name'] is None:
names.append('') # Empty string.
else:
names.append(repo_dict['name'])
plot_dict = {'value': repo_dict['stargazers_count']}
if repo_dict['description'] is not None:
plot_dict['label'] = repo_dict['description']
plot_dicts.append(plot_dict)
在第一部分中,我们添加了一个空字符串来代替任何None
值(因为当你设置 x 标签时你需要一些东西)。
在第二部分中,我们仅使用值创建了 dict,然后仅当描述不是 None
时才将描述添加为标签。
推荐阅读
- vue.js - 有没有人遇到过这个问题?[vue/no-multiple-template-root] 模板根目录不允许 'v-for' 指令.eslint-plugin-vue
- javascript - 根据输入分配javascript表单值
- php - 如何添加 SKU 和产品元数据以及重量以在结帐时显示,如下面的代码
- docker - 在 Kubernetes 中使用 Renovate,例如 Docker-Compose 的 Watchtower
- nginx - 使用 Angular Universal 和 nodejs express 配置 nginx 爬行
- javascript - 将变量从 Python 传递到节点
- python - NLP的工作流程
- microsoft-graph-api - SignInActivity Microsoft 图表不再工作
- python - 在 nextflow 脚本块中导入自己的 python 模块?
- javascript - 如何使用javascript逐个上传文件