Python 和 pylast - AttributeError : 'NoneType' object has no attribute 'encode' python

标签 python api python-2.7 last.fm

我是一个刚刚开始学习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

最佳答案

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

if country.name:
    print country

关于Python 和 pylast - AttributeError : 'NoneType' object has no attribute 'encode' python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14609467/

相关文章:

python - 通过单元测试避免不必要的导入

python - 在 Python 中以字符串格式对日期列表进行排序的最有效方法是什么?

python - 如何在 vim 中启用 python3?

python - wxPython-Python :Serial port problems

python - 如何从元组获得持久哈希?

javascript - 页面正在加载的历史 Api : How to tell the browser,?

ruby-on-rails - 创建裸轨 Controller 类

python - 为什么我的 Python 机器人有时会发布过多的帖子?

python - Pytorch 张量的截断 SVD 分解而不转移到 cpu

python - 使用 Python 到 SYBASE ASE 的 ODBC 连接