AttributeError:'NoneType'对象没有属性'startswith'?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (851)
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")
提问于
用户回答回答于

可以查看[pygal文档] :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")

扫码关注云+社区

领取腾讯云代金券