'NoneType' 对象没有属性 'decode'
'NoneType' object has no attribute 'decode'
我练习编写一些代码以从 GitHub 获取 Python 的顶级存储库,这是我看到的错误:
这是导致上述错误的代码:
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')
你能帮我解决这个问题吗?谢谢
在需要字符串的地方似乎有一个 None
值,回溯似乎显示它是 label
值。
尝试改变这个:
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'],
'xlink': repo_dict['html_url'],
}
为此:
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'] or "",
'xlink': repo_dict['html_url'],
}
我看了你的代码。看起来有些链接没有标签(因此它们的类型为 None)See here。 _compat.py
然后尝试在 None-Type 上调用方法 decode ("utf-8")
,这会导致相应的崩溃。
我建议 plot_dicts 中所有没有标签的条目都用空字符串标记,如下面的代码所示。下面的代码对我有用。
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')
也许您找到了映射列表的更好方法,但这绝对有效。
我练习编写一些代码以从 GitHub 获取 Python 的顶级存储库,这是我看到的错误:
这是导致上述错误的代码:
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')
你能帮我解决这个问题吗?谢谢
在需要字符串的地方似乎有一个 None
值,回溯似乎显示它是 label
值。
尝试改变这个:
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'],
'xlink': repo_dict['html_url'],
}
为此:
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'] or "",
'xlink': repo_dict['html_url'],
}
我看了你的代码。看起来有些链接没有标签(因此它们的类型为 None)See here。 _compat.py
然后尝试在 None-Type 上调用方法 decode ("utf-8")
,这会导致相应的崩溃。
我建议 plot_dicts 中所有没有标签的条目都用空字符串标记,如下面的代码所示。下面的代码对我有用。
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')
也许您找到了映射列表的更好方法,但这绝对有效。