Unicode-encode issues while sending desktop notification using Python
<p>I am fetching latest football scores from a website and sending a notification on the desktop (OS X). I am using BeautifulSoup to scrape the data. I had issues with the unicode data which was generating this error</p>

<pre><code>UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 2: ordinal not in range(128). 
</code></pre>

<p>So I inserted this at the beginning which solved the problem while outputting on the terminal.</p>

<pre><code>import sys 
reload(sys)
sys.setdefaultencoding('utf-8') 
</code></pre>

<p>But the problem exists when I am sending notifications on the desktop. I use terminal-notifier to send desktop-notifications.</p>

<pre><code>def notify (title, subtitle, message):
    t = '-title {!r}'.format(title)
    s = '-subtitle {!r}'.format(subtitle)
    m = '-message {!r}'.format(message)
    os.system('terminal-notifier {}'.format(' '.join((m, t, s))))
</code></pre>

<p>The below images depict the output on the terminal Vs the desktop notification. </p>

<p>Output on terminal.</p>

<p><a href="http://i.stack.imgur.com/cFn41.png" rel="nofollow"><img src="http://i.stack.imgur.com/cFn41.png" alt="enter image description here"></a></p>

<p>Desktop Notification</p>

<p><a href="http://i.stack.imgur.com/MBIN1.png" rel="nofollow"><img src="http://i.stack.imgur.com/MBIN1.png" alt="Dektop Notification"></a></p>

<p>Also, if I try to replace the comma in the string, I get the error,</p>

<p><code>new_scorer = str(new_scorer[0].text).replace(",","")</code></p>

<pre><code>File "live_football_bbc01.py", line 41, in get_score
    new_scorer = str(new_scorer[0].text).replace(",","")
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 2: ordinal not in range(128)
</code></pre>

<p>How do I get the output on the desktop notifications like the one on the terminal? Thanks!</p>

<p><strong><em>Edit : Snapshot of the desktop notification. (Solved)</em></strong> </p>

<p><a href="http://i.stack.imgur.com/OyxkI.png" rel="nofollow"><img src="http://i.stack.imgur.com/OyxkI.png" alt="enter image description here"></a></p>

<p>You are formatting using <code>!r</code> which gives you the <em>repr</em> output, forget the terrible reload  logic and either use unicode everywhere:</p>

<pre><code>def notify (title, subtitle, message):
    t = u'-title {}'.format(title)
    s = u'-subtitle {}'.format(subtitle)
    m = u'-message {}'.format(message)
    os.system(u'terminal-notifier {}'.format(u' '.join((m, t, s))))
</code></pre>

<p>or encode:</p>

<pre><code>def notify (title, subtitle, message):
    t = '-title {}'.format(title.encode("utf-8"))
    s = '-subtitle {}'.format(subtitle.encode("utf-8"))
    m = '-message {}'.format(message.encode("utf-8"))
    os.system('terminal-notifier {}'.format(' '.join((m, t, s))))
</code></pre>

<p>When you call <em>str(new_scorer[0].text).replace(",","")</em> you are trying to encode to <em>ascii</em>, you need to specify the encoding to use:</p>

<pre><code>In [13]: s1=s2=s3= u'\xfc'

In [14]: str(s1) # tries to encode to ascii
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
&lt;ipython-input-14-589849bdf059&gt; in &lt;module&gt;()
----&gt; 1 str(s1)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 0: ordinal not in range(128)

In [15]: "{}".format(s1) + "{}".format(s2) + "{}".format(s3) # tries to encode to ascii---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
&lt;ipython-input-15-7ca3746f9fba&gt; in &lt;module&gt;()
----&gt; 1 "{}".format(s1) + "{}".format(s2) + "{}".format(s3)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 0: ordinal not in range(128)
</code></pre>

<p>You can encode straight away:</p>

<pre><code>In [16]: "{}".format(s1.encode("utf-8")) + "{}".format(s2.encode("utf-8")) + "{}".format(s3.encode("utf-8"))
Out[16]: '\xc3\xbc\xc3\xbc\xc3\xbc'
</code></pre>

<p>Or use use all unicode prepending a u to the format strings and encoding last:</p>

<pre><code>In [17]: out = u"{}".format(s1) + u"{}".format(s2) + u"{}".format(s3)
In [18]: out
Out[18]: u'\xfc\xfc\xfc'

In [19]: out.encode("utf-8")
Out[19]: '\xc3\xbc\xc3\xbc\xc3\xbc'
</code></pre>

<p>If you use <code>!r</code> you are always going to the the bytes in the output:</p>

<pre><code>In [30]: print "{}".format(s1.encode("utf-8"))
Ã¼

In [31]: print "{!r}".format(s1).encode("utf-8")
u'\xfc'
</code></pre>

<p>You can also pass the args using subprocess:</p>

<pre><code>from subprocess import check_call


def notify (title, subtitle, message):
    cheek_call(['terminal-notifier','-title',title.encode("utf-8"),
                '-subtitle',subtitle.encode("utf-8"),
                '-message'.message.encode("utf-8")])
</code></pre>

python unicode utf-8 ascii