chart.render#u to#u文件(';python#u repos.svg';)AttributeError:';非类型';对象没有属性';解码';
我正在学习python,这本书是:“python速成课程:基于项目的编程实践入门”。在《使用API》一书的第17章中,我按照书中所说的做了,但出现了一个错误。我不知道这个错误是什么意思。这是否意味着网站包有问题?如何修复它chart.render#u to#u文件(';python#u repos.svg';)AttributeError:';非类型';对象没有属性';解码';,python,pygal,Python,Pygal,我正在学习python,这本书是:“python速成课程:基于项目的编程实践入门”。在《使用API》一书的第17章中,我按照书中所说的做了,但出现了一个错误。我不知道这个错误是什么意思。这是否意味着网站包有问题?如何修复它 import requests import pygal from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS # Make an API call, and store the res
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'
状态代码:200
存储库总数:2198954
回溯(最近一次呼叫最后一次):
文件“2018-07-13-1.py”,第45行,中
chart.render_到_文件('python_repos.svg'))
文件“C:\Users\Administrator\AppData\Roaming\Python35\site packages\pygal\graph\public.py”,第114行,在render\u to\u文件中
f、 write(self.render(is_unicode=True,**kwargs))
文件“C:\Users\Administrator\AppData\Roaming\Python35\site packages\pygal\graph\public.py”,第52行,在render中
自我设置(**kwargs)
文件“C:\Users\Administrator\AppData\Roaming\Python35\site packages\pygal\graph\base.py”,第217行,在安装程序中
赛尔夫
文件“C:\Users\Administrator\AppData\Roaming\Python35\site packages\pygal\graph\graph.py”,第933行,在
self._plot()
文件“C:\Users\Administrator\AppData\Roaming\Python35\site packages\pygal\graph\bar.py”,第146行,在\u绘图中
自助酒吧(系列)
文件“C:\Users\Administrator\AppData\Roaming\Python35\site packages\pygal\graph\bar.py”,第116行,在bar中
元数据)
文件“C:\Users\Administrator\AppData\Roaming\Python35\site packages\pygal\util.py”,第233行,在
元数据['label'])
文件“C:\Users\Administrator\AppData\Roaming\Python35\site packages\pygal\\u compat.py”,第61行,以unicode格式
返回字符串。解码('utf-8')
AttributeError:“非类型”对象没有属性“解码”
该错误是由于您在图表上设置的标签之一为None
,这是其中之一。当pygal呈现图表时,它希望标签是一个字符串,并调用decode
函数。None
对象上不存在此函数,因此会引发错误
查看Github存储库的名称和描述(您分别将其设置为x标签和数据标签),我们可以看到其中一个存储库的描述为None
。我不会说这本书中的代码有什么特别的错误,只是运气不好,当时最著名的Python存储库之一没有任何描述
我们可以通过检查for循环中的None
值来解决此问题:
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
时才将描述添加为标签。该错误基本上意味着您的文件python\u repos.svg
不包含任何内容
H=''
names, plot_dicts = [],[]
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
if repo_dict['description']is not None:
H = repo_dict['description']
plot_dict = {
'value': repo_dict['stargazers_count'],
'label':H,
}
plot_dicts.append(plot_dict)