Hexadecimal list conversion

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Dec 20 08:01:29 EST 2007


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




More information about the Python-list mailing list