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")
任何帮助都是非常感谢的。
发布于 2018-08-23 04:59:12
@SagarJhamb你的代码有两个缺陷
1.repo_dicts未初始化
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")
https://stackoverflow.com/questions/51974020
复制相似问题