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>Use: Ësys.getfilesystemencoding` to get your encoding</p>

<p>Encode your string with it, ignore or replace errors:</p>

<pre><code>import sys

encoding = sys.getfilesystemencoding()
msg = new_scorer[0].text.replace(",", "")
print(msg.encode(encoding, errons="replace"))
</code></pre>

python unicode utf-8 ascii