'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: this is the code which causes above stated error: import requests import p
Solution 1:
There seems to be a None
value somewhere where a string is expected, and the traceback seems to show it is the label
value.
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'],
}
Solution 2:
I looked at your code. It looks like some links have no label (so they are of type None) See here. _compat.py
then tries to invoke the method decode ("utf-8")
on a None-Type, which leads to the corresponding crash.
I recommend all entries in plot_dicts that have no label to be labeled with an empty string like shown in the code below. 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 heredeff(e):
if e['label'] isNone:
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.
Post a Comment for "'nonetype' Object Has No Attribute 'decode'"