【问题标题】:AttributeError: 'NoneType' object has no attribute 'startswith'AttributeError:“NoneType”对象没有属性“startswith”
【发布时间】:2018-08-22 19:38:42
【问题描述】:
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS


url = "https://api.github.com/search/repositories? 
q=language:python&sort=stars"
r = requests.get(url)

response_dict = r.json()

names,stars = [],[]
for repo in repo_dicts:
    names.append(repo["name"])
    stars.append(repo["stargazers_count"])

my_style = LS("333366", base_style=LCS)
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
chart.title = "Most starred Python projects on GitHub"
chart.x_labels = names

chart.add(" ", stars)
chart.render_to_file("repo_visual.svg")

运行此代码时,我得到一个 AttributeError。 我正在尝试使用 pygal 模块将星星最多的 python 项目绘制到条形图上。该任务来自 Eric Matthes 的 Python 速成课程。我正在和他的代码交叉检查我的代码,我似乎找不到任何问题

追踪:

Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/generatingdata/python_repos.py", line 
51, in <module>
chart.render_to_file("x.svg")

非常感谢任何帮助。

【问题讨论】:

  • 欢迎来到 StackOverflow!包含堆栈跟踪通常会有所帮助,这样导致错误的行就很明显了。

标签: python pygal


【解决方案1】:

@SagarJhamb 你的代码有两个错误
1.repo_dicts 未初始化
2.定义 my_style= LS("333366", base_style=LCS) 值应以 #333366 开头,以了解有关使用 pygal 自定义样式的更多信息,您可以查看 [pygal 文档][1][1]: http://www.pygal.org/en/stable/documentation/parametric_styles.html

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS


url = "https://api.github.com/search/repositories?q=language:python&sort=stars"
r = requests.get(url)

response_dict = r.json()
# initialise response dict
repo_dict=response_dict['items']

names,stars = [],[]
for repo in repo_dict:
   names.append(repo["name"])
   stars.append(repo["stargazers_count"])
# #333366 remove your error of NoneType object has no attribute startswith
#It is rightformat of using custom style with pygal
#It should starts with #
my_style = LS("#333366", base_style=LCS)
chart = pygal.Bar( style=my_style, x_label_rotation=45, show_legend=False)
chart.title = "Most starred Python projects on GitHub"
chart.x_labels = names
chart.add("stars", stars)
chart.render_to_file("repo_visual.svg")

【讨论】:

  • 感谢您的回复! repo_dicts 已初始化我只是忘记在我的帖子中包含这部分代码,因为它是早期程序的一部分。我完全忘记了 RGB 值前面的 # 符号。包括那行得通,再次感谢。
猜你喜欢
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 2013-07-29
  • 2019-01-01
  • 2021-12-26
相关资源
最近更新 更多