我是一个刚刚开始学习Python的完整菜鸟。我正在使用Last.fm的pylast库来提取用户邻居的列表。他们的一些属性,但当我尝试打印邻居的国家时,我收到以下错误:
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
答案 0 :(得分:2)
缺少国家/地区名称,但pylast
库未正确处理该情况。你必须自己测试空的国家案例:
if country.name:
print country
答案 1 :(得分:0)
我已在我的fork of pylast中解决此问题。
当您致电country = i.get_country()
时,它现在会返回None
,而不是Country
name
None
的{{1}}对象。