[Tutor] dbus.Array to string

Amit Saha amitsaha.in at gmail.com
Tue Aug 13 17:59:29 CEST 2013


On Mon, Aug 12, 2013 at 1:13 PM, eryksun <eryksun at gmail.com> wrote:
> On Sun, Aug 11, 2013 at 10:34 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On 12/08/13 11:40, eryksun wrote:
>>> Typically strings should be unicode. If the byte sequence is Latin-1
>>> (including ASCII), you can map unichr() and join the characters with
>>> u''.join(); that's equivalent to chr() and ''.join() in 3.x. More
>>> generally, decode() the byte string. A simply way that works in 2.6+
>>> is to create a bytearray:
>>>
>>>      >>> bytearray(ssid).decode('latin-1')
>>>      u'BigPond679D85'
>>
>> But, what makes you think that the encoding will be latin-1? If I had to
>> guess an encoding, I'd guess UTF-8.
>
> Unicode ordinals in the 8-bit range are Latin-1 (aka ISO-8859-1, i.e.
> Basic Latin and Latin-1 Supplement, including C0 and C1 control
> codes). You can always decode as Latin-1 without getting a decoding
> error. UTF-8 is multibyte and defaults to raising a decoding error if
> the sequence isn't valid.
>
> It's up to Amit to figure out what the encoding is. It's D-Bus, so
> probably Linux. In that case, UTF-8 is a good guess.

Nice, thanks. What does it mean (and will it always work?) when I
don't specify any encoding:

>>> bytearray(ssid).decode()
u'BigPond679D85'


The default encoding (on Python 2) is ascii:

>>> import sys
>>> sys.getdefaultencoding()
'ascii'

Thanks,
Amit.

-- 
http://echorand.me


More information about the Tutor mailing list