繁体   English   中英

'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)。 请参见此处 _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')

也许您找到了映射列表的更好方法,但这绝对可行。

似乎在期望字符串的某处有一个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'],
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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