Python&;pylast-属性错误:';非类型';对象没有属性';编码';python

Python&;pylast-属性错误:';非类型';对象没有属性';编码';python,python,api,python-2.7,last.fm,Python,Api,Python 2.7,Last.fm,我是个彻头彻尾的傻瓜,刚刚开始学习Python。我正在使用pylast library for Last.fm来提取用户邻居的列表&它们的一些属性,但是当我试图打印邻居的国家时,我得到以下错误: thrillofme None 0 Traceback (most recent call last): File "/Users/Moi/DSR/Week 2/My tutorials/my-lastfm-thing-3.py", line 24, in <module> pr

我是个彻头彻尾的傻瓜,刚刚开始学习Python。我正在使用pylast library for Last.fm来提取用户邻居的列表&它们的一些属性,但是当我试图打印邻居的国家时,我得到以下错误:

thrillofme
None
0
Traceback (most recent call last):
  File "/Users/Moi/DSR/Week 2/My tutorials/my-lastfm-thing-3.py", line 24, in <module>
    print country
  File "/Library/Python/2.7/site-packages/pylast.py", line 944, in r
    return _string(funct(*args))
  File "/Library/Python/2.7/site-packages/pylast.py", line 3497, in _string
return text.encode("utf-8")
AttributeError: 'NoneType' object has no attribute 'encode'

缺少国家/地区名称,但
pylast
库未正确处理该情况。您必须自己测试空国家/地区案例:

if country.name:
    print country
我已经把它修好了


当您调用
country=i.get_country()
时,它现在返回
None
而不是
country
对象,其中
name
None

,它看起来像是决定为对象打印什么的方法(
\u repr\uu
)为相邻对象设置时,假定各种属性不是
None
。您是否可能正在查看此用户的邻居,而这些邻居实际上没有列出他们的国家?如果用户未填写,国家/地区字段会报告什么?如果它是
None
,并且last.fm API不能处理此问题,则必须使用
try:。。。除了:
或其他形式的异常处理来避免它。就是这样!工作起来很有魅力。谢谢!
if country.name:
    print country