[英]'NoneType' object has no attribute 'decode'
I practice writing some code to get top repositories of Python from GitHub and this is the error I see: 我练习编写一些代码来从GitHub获取Python的顶级存储库,这是我看到的错误:
this is the code which causes above stated error: 这是导致上述错误的代码:
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
path = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(path)
response_dict = r.json()
# 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'],
'xlink': repo_dict['html_url'],
}
plot_dicts.append(plot_dict)
my_style = LS('#333366', base_style=LCS)
# Make visualization.
my_config = pygal.Config()
chart = pygal.Bar(my_config, style=my_style)
my_style = LS('#333366', base_style=LCS)
chart.title = 'Most-Starred Python Projects on GitHub'
chart.x_labels = names
chart.add('', plot_dicts)
chart.render_to_file('python_repos.svg')
Can you help me with this please. 你能帮我这个忙吗? Thank you
谢谢
I looked at your code. 我看着你的代码。 It looks like some links have no label (so they are of type None) See here .
看起来有些链接没有标签(因此它们的类型为None)。 请参见此处 。
_compat.py
then tries to invoke the method decode ("utf-8")
on a None-Type, which leads to the corresponding crash. _compat.py
然后尝试对None-Type调用方法decode ("utf-8")
,这将导致相应的崩溃。
I recommend all entries in plot_dicts that have no label to be labeled with an empty string like shown in the code below. 我建议plot_dicts中所有没有标签的条目都用空字符串标记,如下面的代码所示。 The code below works for me.
下面的代码对我有用。
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
path = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(path)
response_dict = r.json()
# 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'],
'xlink': repo_dict['html_url'],
}
plot_dicts.append(plot_dict)
my_style = LS('#333366', base_style=LCS)
# Make visualization.
my_config = pygal.Config()
chart = pygal.Bar(my_config, style=my_style)
my_style = LS('#333366', base_style=LCS)
chart.title = 'Most-Starred Python Projects on GitHub'
chart.x_labels = names
# preprocess labels here
def f(e):
if e['label'] is None:
e['label'] = ""
return e
plot_dicts = list(map(f, plot_dicts))
chart.add('', plot_dicts)
chart.render_to_file('python_repos.svg')
Maybe you find a better way to map the list but that definitely works. 也许您找到了映射列表的更好方法,但这绝对可行。
There seems to be a None
value somewhere where a string is expected, and the traceback seems to show it is the label
value. 似乎在期望字符串的某处有一个
None
值,并且回溯似乎表明它是label
值。
Try to change this: 尝试更改此:
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'],
'xlink': repo_dict['html_url'],
}
To this: 对此:
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'] or "",
'xlink': repo_dict['html_url'],
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.