简体   繁体   中英

'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 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 . _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 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.

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'],
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM