[Tutor] Struct and UTF-16

Kent Johnson kent37 at tds.net
Mon Oct 3 14:10:14 CEST 2005


Liam Clarke wrote:
> So, using
> 
> val = unicode(value)
> self._slaveMap[attr].setPayload(value.encode("UTF-16"))
> 
> I can stick normal strings in happily. Of course, as you mentioned,
> Kent, this leaves me vulnerable if the string differs to
> sys.getdefaultencoding().
> 
> Other than directly from the user, the most likely source of data will
> be from pyid3lib, which for the time being assumes all strings are
> ISO-8859-1.

You can decode with iso-8859-1 instead of ascii:
val = unicode(value, 'iso-8859-1')
self._slaveMap[attr].setPayload(value.encode("UTF-16"))

you could even allow client code to tell you the encoding:

def setPayload(self, val, encoding='iso-8859-1'):
  if not isinstance(value, unicode):
    value = unicode(value, encoding)
  self._slaveMap[attr].setPayload(value.encode("UTF-16"))

Kent



More information about the Tutor mailing list