pygal制图“AttributeError: 'NoneType' object has no attribute 'decode'《Python编程:从入门到实践》

在阅读《Python编程:从入门到实践》时,作者在使用pygal绘制柱状图时遇到了'AttributeError: 'NoneType' object has no attribute 'decode''的错误。经过代码审查,发现是由于接口返回的数据为空导致的。解决方案是在代码中为可能为空的部分设置默认值,以增强代码的健壮性。
摘要由CSDN通过智能技术生成

pygal制图“AttributeError: ‘NoneType’ object has no attribute ‘decode’《Python编程:从入门到实践》


最近在刷《Python编程:从入门到实践》一书 webAPI 部分使用github接口绘制柱状图
遇到此报错 通过核对书中代码 发现并无错误

pygal制图“AttributeError: 'NoneType' object has no attribute 'decode'

通过分析是 接口中 description为空原因 ps 原来大神代码也有不健壮的时候啊

完整代码:

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
#  执行api调用并储存响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code:", r.status_code)

#  将api响应储存在一个变量中
response_dict = r.json()

#  处理结果 打印字典的键
print('总数', 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值