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'],
'xlink': repo_dict['owner']['html_url'],
}
plot_dicts.append(plot_dict)
my_style = LS('#333366', base_style=LCS)
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
chart.title = 'Most-Stared Python Project On Github'
chart.x_labels = names
chart.add('', plot_dicts)
chart.render_to_file('new_repos.svg')
*如果我运行此命令,则会出错。
Traceback (most recent call last):
File "new_repos.py", line 54, in <module>
chart.render_to_file('new_repos.svg')
File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\public.py", line 114, in render_to_file
f.write(self.render(is_unicode=True, **kwargs))
File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\public.py", line 52, in render
self.setup(**kwargs)
File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\base.py", line 217, in setup
self._draw()
File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\graph.py", line 933, in _draw
self._plot()
File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\bar.py", line 146, in _plot
self.bar(serie)
File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\bar.py", line 116, in bar
metadata)
File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\util.py", line 234, in decorate
metadata['label'])
File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\_compat.py", line 62, in to_unicode
return string.decode('utf-8')
AttributeError: 'NoneType' object has no attribute 'decode'
*如果我将部分代码更改为此,错误将消失,*但是图表中将忽略描述。
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'],
'xlink': repo_dict['owner']['html_url'],
}
plot_dicts.append(plot_dict)
*我不知道为什么,任何人都可以帮我解决这个问题吗?
答案 0 :(得分:2)
可能是API与描述不符,因此您可以使用ifelse
来解决它。就像这样。
description = repo_dict['description']
if not description:
description = 'No description provided'
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': description,
'xlink': repo_dict['html_url'],
}
plot_dicts.append(plot_dict)`
如果Web API返回值null
,则表示没有结果,因此您可以使用if语句解决它。也许在这里,项shadowsocks
没有返回任何东西,所以它可能出了问题。
答案 1 :(得分:1)
'label':str(repo_dict['description'])
尝试上面的str(),看起来你之前得到的数据并没有明确说明'描述'的价值类型。
答案 2 :(得分:0)
from requests import get
from pygal import Bar, style, Config
url = "https://api.github.com/search/repositories?q=language:python&sort=stars"
# получение данных по API GitHub
get_data = get(url)
response_dict = get_data.json()
repositories = response_dict['items']
# получение данных для построения визуализации
names, stars_labels = [], []
for repository in repositories:
names.append(repository['name'])
if repository['description']:
stars_labels.append({'value': repository['stargazers_count'],
'label': repository['description'],
'xlink': repository['html_url']})
else:
stars_labels.append({'value': repository['stargazers_count'],
'label': "нет описания",
'xlink': repository['html_url']})
# задание стилей для диаграммы
my_config = Config()
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.truncate_label = 15 # сокращение длинных названий проектов
my_config.show_y_guides = False # скроем горизонтальные линии
my_config.width = 1300
my_style = style.LightenStyle('#333366', base_style=style.LightColorizedStyle)
my_style.label_font_size = 16
my_style.major_label_font_size = 20
# построение визуализации
chart = Bar(my_config, style=my_style)
chart.title = "Наиболее популярные проекты Python на GitHub"
chart.x_labels = names
chart.add('', stars_labels)
chart.render_to_file("python_projects_with_high_stars.svg")