Python与pylast - AttributeError: 'NoneType'对象没有'encode'属性
我刚开始学习Python,还是个完全的新手。我在玩一个叫pylast的库,它是用来从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'
我查看了一些其他解决这个错误信息的方法,感觉country
的编码可能不太正确,所以无法打印出来,但我不知道该怎么处理。任何帮助都非常感谢!这是我的代码。
import pylast
api_key = "XXX"
username = "Strangelove"
network = pylast.LastFMNetwork(api_key = api_key)
user = pylast.User(username, network)
# Let's pull a list of the specified user's Last.fm neighbours.
# Neighbours are users with a similar taste in music.
neighbours = user.get_neighbours()
for i in neighbours:
gender = i.get_gender()
age = i.get_age()
country = i.get_country()
print i
print gender
print age
print country
2 个回答
1
我在我的pylast的分支中修复了这个问题。
现在,当你调用country = i.get_country()
时,它会返回None
,而不是一个Country
对象,那个对象的name
是None
。
2
这里提到国家名称缺失了,但是 pylast
这个库没有正确处理这种情况。你需要自己来检查国家名称是否为空:
if country.name:
print country