Pygal cartography "Attributeerror: ' Nonetype ' object has no attribute ' decode '"

Source: Internet
Author: User

Recently looking at <python into: 17 chapters from Getting started to practice > using the Web Api

17.2.3 based on data drawing

# Coding=utf-8 Import requests import Pygal from Pygal.style import Lightcolorizedstyle as LCS, Lightenstyle as LS # Executive API
Call and store response URL = ' Https://api.github.com/search/repositories?q=language:python&sort=star ' r = Requests.get (URL) Print ("Status code:", R.status_code) # stores the API response in a variable response_dict = R.json () print ("Total repositories:", Response_ dict[' Total_count '] # study the information about the warehouse repo_dicts = response_dict[' items '] print "Number of items:", Len (repo_dicts) Names,plot _dicts=[],[] for repo_dict in Repo_dicts:names.append (repo_dict[' name ']) if repo_dict[' description ': PLO t_dict={' value ': repo_dict[' stargazers_count ', ' label ': repo_dict[' description '], ' XL Ink ': repo_dict[' Html_url ']} plot_dicts.append (plot_dict) else:plot_dict={' value ':
        repo_dict[' Stargazers_count '], ' label ': ' ABC ', ' xlink ': repo_dict[' Html_url ']} Plot_dicts.append (plot_dict) # visualization 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 (', plot_dicts) print plot_dicts chart.render_to_file (' python_repos.svg ')

The code above is a little different from the book, mainly

For repo_dict in repo_dicts:
    names.append (repo_dict[' name '])
    if repo_dict[' description ':
        plot_dict={' Value ': repo_dict[' Stargazers_count '],
                   ' label ': repo_dict[' description ',
                   ' xlink ': repo_dict[' Html_url ']
                   }
        Plot_dicts.append (plot_dict)
    else:
        plot_dict={' value ': repo_dict[' Stargazers_count '],
                   ' label ': ' ABC ' ,
                   ' xlink ': repo_dict[' Html_url ']}
        plot_dicts.append (plot_dict)

Why add this paragraph.

According to the book, the following error

Traceback (most recent): File "c:/users/vivi01.zhu/pycharmprojects/untitled2/python_repos.py", line ;module> chart.render_to_file (' python_repos.svg ') file "C:\Python27\lib\site-packages\pygal\graph\public.py", Line 114, in Render_to_file f.write (Self.render (Is_unicode=true, **kwargs)) file "C:\Python27\lib\site-packages\pyga l\graph\public.py ", line-render Self.setup (**kwargs) File" C:\Python27\lib\site-packages\pygal\graph\base.py "
    , line 217, in the Setup self._draw () File "C:\Python27\lib\site-packages\pygal\graph\graph.py", line 933, in _draw Self._plot () file "C:\Python27\lib\site-packages\pygal\graph\bar.py", line 146, in _plot self.bar (serie) file "C:\ python27\lib\site-packages\pygal\graph\bar.py ", line 116, in bar metadata) File" C:\Python27\lib\site-packages\pygal \util.py ", line 233, in decorate metadata[' label ') File" C:\Python27\lib\site-packages\pygal\_compat.py ", line 61, In To_unicode return StRing.decode (' Utf-8 ') attributeerror: ' Nonetype ' object has no attribute ' decode ' 

Plot_dicts that need to be shown on the diagram can be printed and normal

The guess is that there is a parameter (Repo_dict's description) of None, no attributes, a if--else judge of whether this parameter is none, run through, bingo~

(' Status code: ')
(' Total repositories: ', 1923446)
Number of items:30
[{' XLink ': U ' https://github.com/vinta/awesome-python ', ' value ': 38175, ' label ': U ' A curated list of awesome Python frameworks, libraries, software and resources} ......... {' XLink ': U ' https://github.com/drduh/macOS-Security-and-Privacy-Guide ', ' value ': 11824, ' label ': U ' A practical Guide to securing MacOS. '}]

Process finished with exit code 0


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.