"Gabriel Genellina" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > En Thu, 20 Dec 2007 09:30:14 -0300, Neil Webster <[EMAIL PROTECTED]> > 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 -- http://mail.python.org/mailman/listinfo/python-list