【问题标题】:'NoneType' object has no attribute 'decode'“NoneType”对象没有“解码”属性
【发布时间】:2019-07-04 14:59:23
【问题描述】:

我练习编写一些代码以从 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')

请你帮我解决这个问题。谢谢

【问题讨论】:

  • 请使用代码格式进行回溯。它们在报价格式中非常难以阅读
  • 我很抱歉,但我只是添加了一张跟踪簿的图片,所以要求我在代码格式化跟踪后提供更多信息

标签: python visualization pygal


【解决方案1】:

在需要字符串的地方似乎有一个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'],
}

【讨论】:

  • 谢谢@olinox14,它现在工作正常。如何更好地阅读回溯?如果您不指出这个问题,这些回溯对我没有任何帮助。
  • line 233, in decorate metadata['label'])这乍一看很难理解
  • 回溯确实很难阅读,但是在这种情况下您可以做的是使用调试器并在有问题的行上添加断点。这应该会在 95% 的情况下引导您找到解决方案...
【解决方案2】:

我查看了您的代码。看起来有些链接没有标签(因此它们的类型为无)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')

也许您找到了一种更好的方法来映射列表,但这绝对有效。

【讨论】:

  • 正如我刚刚看到的,您已经有了答案。嗯,就是这样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 2021-11-02
  • 2014-07-05
  • 2018-05-05
  • 2013-02-11
相关资源
最近更新 更多