首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >AttributeError:'NoneType‘对象没有'startswith’属性

AttributeError:'NoneType‘对象没有'startswith’属性
EN

Stack Overflow用户
提问于 2018-08-23 03:38:42
回答 1查看 3.4K关注 0票数 0
代码语言:javascript
复制
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速成课程。我正在交叉检查我的代码和他的代码,我似乎没有发现任何问题

跟踪:

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

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-23 04:59:12

@SagarJhamb你的代码有两个缺陷

1.repo_dicts未初始化

  1. 定义my_style= LS("333366",base_style=LCS)该值应以#333366开头要了解有关pygal自定义样式的更多信息,请查看 documentation1:

代码语言:javascript
复制
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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51974020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档