我是一个相对新手,正在学习编程。我尝试在Ubuntu(Linux)上编写Spotify的Snip的Python等效程序,但遇到了一些问题。我可以正确地编码标题,但无法以同样的方式编码艺术家信息。
当我尝试以同样的方式编码艺术家信息时,出现以下错误:
当我尝试以同样的方式编码艺术家信息时,出现以下错误:
File "./songfinder.py", line 11, in currentplaying
artiststr = str((metadata['xesam:artist']).encode('utf-8'))
AttributeError: 'dbus.Array' object has no attribute 'encode'
然而,标题的处理方式完全相同且有效。
迄今为止的代码是有效的,但例如会出现\xd8而非Ø等类似情况:
import dbus
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
spotify_properties = dbus.Interface(spotify_bus, "org.freedesktop.DBus.Properties")
def currentplaying():
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
title = str((metadata['xesam:title']).encode('utf-8'))
artiststr = str((metadata['xesam:artist']))
if ("dbus.string" in artiststr.lower()):
artists = artiststr.split("(u")
artist = artists[1]
artists = artist.split(")],")
artist = artists[0]
artist = artist.replace("(u", "")
else:
artist = "'unknown'"
artist = (artist.replace("'",""))
playing = (artist + " - " + title + " ")
return playing
#save playing to file.txt
相关问题和答案:
如何在Python中替换Unicode字符串中的非ASCII字符为什么它不能解决我的问题: 我想打印/保存实际的字符,而不是用类似的字符替换它