簡體   English   中英

AttributeError:'NoneType'對象沒有屬性'startswith'

[英]AttributeError: 'NoneType' object has no attribute 'startswith'

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")

任何幫助是極大的贊賞。

@SagarJhamb您的代碼有兩個錯誤
1.repo_dicts未初始化
2.定義my_style = LS(“ 333366”,base_style = LCS)值應以#333366開頭,以了解有關pygal的自定義樣式的更多信息,您可以查看[pygal文檔] [1] [1]: http:// www .pygal.org / EN /穩定/文檔/ 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM