對Github上Python開源項目進行分析時遇到的一個AttributeError的解釋及其解決方法。
最近在分析Github的Python開源項目時候遇到了一些問題,直接上傳代碼段:
import requestsnimport pygalnfrom pygal.style import LightColorizedStyle as LCS, LightenStyle as LSnnn# 執行API調用並存儲nurl = https://api.github.com/search/repositories?q=language:python&sort=starsnr = requests.get(url)nprint("Status code:", r.status_code)nn# 將API響應存儲在一個變數裡面nresponse_dict = r.json()nprint("Total repositories:", response_dict[total_count])nn# 探索有關倉庫的信息nrepo_dicts = response_dict[items]nprint("Repositories returned:", len(repo_dicts))nn# # 研究第一個倉庫n# repo_dict = repo_dicts[0]n#n# # 列印各種信息n# print("nSelected information about first repository:")n# for repo_dict in repo_dicts:n# print(Name:, repo_dict[name])n# print(Owner:, repo_dict[owner][login])n# print(Stars:, repo_dict[stargazers_count])n# print(Repository:, repo_dict[html_url])n# print(Created:, repo_dict[created_at])n# print(Updated:, repo_dict[updated_at])n# print(Description, repo_dict[description])nnnames, plot_dicts = [], []nfor repo_dict in repo_dicts:n names.append(repo_dict[name])nn plot_dict = dict(value=repo_dict[stargazers_count], label=repo_dict[description])n plot_dicts.append(plot_dict)nn# 可視化nmy_style = LS(#333366, base_style=LCS)nnmy_config = pygal.Config()nmy_config.x_label_rotation = 45nmy_config.show_legend = Falsenmy_config.title_font_size = 24nmy_config.label_font_size = 14nmy_config.major_label_font_size = 18nmy_config.truncate_label = 15nmy_config.show_y_guides = Falsenmy_config.width = 1000nnchart = pygal.Bar(my_config, style=my_style)nchart.title = Most-Starred Python Projects on Githubnchart.x_labels = namesnnchart.add(, plot_dicts)nchart.render_to_file(python_repos.svg)n
運行上述代碼,遇到了如下錯誤:
/usr/bin/python3.5 /home/linuxer/code/date_visual/python_repos.pynStatus code: 200nTotal repositories: 1995979nRepositories returned: 30nTraceback (most recent call last):n File "/home/linuxer/code/date_visual/python_repos.py", line 58, in <module>n chart.render_to_file(python_repos.svg)n File "/usr/local/lib/python3.5/dist-packages/pygal/graph/public.py", line 114, in render_to_filen f.write(self.render(is_unicode=True, **kwargs))n File "/usr/local/lib/python3.5/dist-packages/pygal/graph/public.py", line 52, in rendern self.setup(**kwargs)n File "/usr/local/lib/python3.5/dist-packages/pygal/graph/base.py", line 217, in setupn self._draw()n File "/usr/local/lib/python3.5/dist-packages/pygal/graph/graph.py", line 933, in _drawn self._plot()n File "/usr/local/lib/python3.5/dist-packages/pygal/graph/bar.py", line 146, in _plotn self.bar(serie)n File "/usr/local/lib/python3.5/dist-packages/pygal/graph/bar.py", line 116, in barn metadata)n File "/usr/local/lib/python3.5/dist-packages/pygal/util.py", line 233, in decoraten metadata[label])n File "/usr/local/lib/python3.5/dist-packages/pygal/_compat.py", line 61, in to_unicoden return string.decode(utf-8)nAttributeError: NoneType object has no attribute decoden
當時猜測這個原因是可能有個模塊有問題。
但是後來發現不會是這個原因,結合錯誤 NoneType object has no attribute decode ,應該是某個變數沒有指明屬性。
於是嘗試加上 str() 嘗試,將其中 label=repo_dict[description] 替換為label=str(repo_dict[description]) .
於是修正後代碼,重新編譯:
/usr/bin/python3.5 /home/linuxer/code/date_visual/python_repos.pynStatus code: 200nTotal repositories: 1995996nRepositories returned: 30nnProcess finished with exit code 0n
成功編譯成功:
於是就可以繼續進行下一步的數據分析。
有些地方屬性沒指明,也可能是空值,所以要對其進行處理,不然會遇到錯誤。
謝謝各位關注。中秋節快樂!
推薦閱讀:
※基於pytesseract的簡單驗證碼識別
※Python入門 面向對象編程基礎
※Python入門到精通視頻課程(5)
※Python 繪製分形圖(曼德勃羅集、分形樹葉、科赫曲線、分形龍、謝爾賓斯基三角等)附代碼
※人生若只如初見,何必找包爬數據