Python; How to replace escaped non-unicode characters with their respective 'real' utf-8
<p>I am relatively new to programming, and I have a small problem writing a python equivalent of Snip for spotify for ubuntu(linux)
Somehow i can encode the title correctly, but am unable to encode the artist the same way</p>

<p>when i try to encode the artist in the same fashion i get this:</p>

<pre><code>File "./songfinder.py", line 11, in currentplaying
    artiststr = str((metadata['xesam:artist']).encode('utf-8'))
AttributeError: 'dbus.Array' object has no attribute 'encode'
</code></pre>

<p>however the title is done exactly the same and that is working.</p>

<p>Code so far IS working but has for example \xd8 instead of Ã, and similar:</p>

<pre><code>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
</code></pre>

<p>relevant qna's:
<a href="http://stackoverflow.com/questions/3704731/replace-non-ascii-chars-from-a-unicode-string-in-python">Replace non-ascii chars from a unicode string in Python</a></p>

<p>Why it does not resolve my problem: I would like to print/save the actual character, not replace it with similar ones</p>

<p>The error you getting is not about unicode, it is about wrong type. Python complains that you trying to call string method <code>encode</code> from the array object. Which does not have this method. </p>

<p>The first this I would try is to remove redundant brackets here it getting artiststr like this: <code>artiststr = str(metadata['xesam:artist'])</code>.</p>

<p>But I'm not sure this would work. If it doesn't work, you need to find out what type has metadata['xesam:artist']. Looks like it is not string, but array. So you need to fix the code which fills <code>metadata['xesam:artist']</code> with data. You can try to use debugger or just <code>print()</code> function to find out the content of <code>metadata['xesam:artist']</code>. Or provide the relevant code in you question too.</p>

python unicode encoding utf-8 dbus