Hexadecimal list conversion

Mark T nospam at nospam.com
Thu Dec 20 10:34:08 EST 2007


"Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> wrote in message 
news:mailman.2617.1198155855.13605.python-list at python.org...
> En Thu, 20 Dec 2007 09:30:14 -0300, Neil Webster <nswebster at gmail.com> 
> escribi�:
>
>> I have a list which is a line from a file:
>> ['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x002\x005\x009\x009\x00',
>> '\x002\x001\x003\x006\x002\x002\x00.\x001\x007\x004\x002\x008\x002\x00']
>>
>> This should be in the format:
>> ['381475.502599', '213622.174282']
>>
>> I've tried a few options using replace (replacing "\x00" with "") and
>> trying to convert from hexademical to decimal.
>
> The replace works:
>
> py> for item in L:
> ...   print item.replace('\x00','')
> ...
> 381475.502599
> 213622.174282
>
> If you got that from a file, I bet you read it using the wrong encoding. 
> Try opening the file using codecs.open("filename", "rb", 
> encoding="utf-16-be") instead of plain open. When your read it, you'll get 
> unicode objects instead of strings, but with the right contents. If you 
> wish you can convert to strings using 
> line_read.encode(your_system_encoding); if all your data is numeric the 
> encoding used is irrelevant and can be omited.
>
> -- 
> Gabriel Genellina
>

There is an odd number of bytes in each string.  Each begins and ends with 
\x00, so it doesn't look like utf-16-be.  But replace works:

>>> L=['\x003\x008\x001\x004\x007\x005\x00.\x005\x000\x002\x005\x009\x009\x00','\x002\x001\x003\x006\x002\x002\x00.\x001\x007\x004\x002\x008\x002\x00']
>>> [s.replace('\x00','') for s in L]
['381475.502599', '213622.174282']

-Mark Tolonen




More information about the Python-list mailing list