views:

310

answers:

2

I have a debug app I've been writing which receives data from a C-based process via UDP. One of the strings sent to me contains a '°' character - http://en.wikipedia.org/wiki/Degree_symbol">Unicode U+00B0 (which incidentally breaks the StackOverflow search function!). When my wxPython application tries to append that string to a text box I get a UnicodeDecodeError.

My first attempt to fix the issue simply caught that error (because the app apparently does send some bad messages. The problem is that the app also uses the character to report various temperatures around the unit and that's something we really need to log. Changing the source app is out of my control, so how can I detect and decode those symbols into something the wxTextCtrl can display?

+1  A: 

I can't say mych about wxPython itself, but I am guessing that it is trying to convert the text to Unicode before displaying it, If you have a string like '123\xB0' and try to convert it to Unicode with teh default encoding (ASCII) then it will throw UnicodeDecodeError. You can probably fix this by replacing

s = message.get_string()

with

s = message.get_string().decode('ISO8859-1')

(where I am assuming a function get_string() that gets the message as a string). The difference here is that by handong the conversion to Unicode yourself you get to specify the encoding.

pdc
+2  A: 

pdc got it right, the following works fine (but fails without the decode):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx

app = wx.PySimpleApp()
app.TopWindow = wx.Frame(None)
field = wx.TextCtrl(app.TopWindow)
field.Value += '°'.decode('ISO8859-1')
app.TopWindow.Show()
app.MainLoop()
Toni Ruža
Just in case this is a Windows application, you better use encoding "cp1252" instead. It doesn't matter if you only receive degree signs (it's the same in both).
ΤΖΩΤΖΙΟΥ