gonzlobo wrote: > I've been using Python for a few days. It's such the perfect language > for parsing data! > > I really like it so far, but I'm having a hard time reading a file, > reading the first few hex characters & converting them to an integer. > Once the characters are converted to an integer, I'd like to write the > data to another file. > > Here's the code snipped to the bare minimum: > --- > # Open File > AP_File= open("AP.txt", "r") > decoded_File= open("decoded.txt", "w") > > # read & process data line by line > for line in AP_File: > time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely > hosed! > decodedFile.write(time) > > #close files > AP_File.close() > decoded_File.close() > --- > AP.txt > 000000d5 26 0600 80 00 ec 80 02 03 7d db 02 33 > 000000d5 26 0601 80 00 80 00 02 37 fe 54 01 09 > 000000d5 06 0602 80 00 e0 00 01 29 fe d2 69 99 > 000000d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99 > 000000d5 26 0604 80 00 fe 54 02 09 80 00 01 5d > 000000d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b > 000000d5 4a 0610 81 00 86 00 02 26 12 00 02 a6 > 000000d5 4f 0611 00 00 00 50 00 00 00 00 07 00 > 000000d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b > 000000d5 0a 0613 08 5c 04 88 08 98 00 00 00 00 > 000000d5 06 0614 80 00 e0 00 02 01 60 79 82 2b > 000000d5 0a 0615 08 00 00 00 00 00 00 00 00 00 > 000000d5 26 0616 80 00 80 00 02 5d 04 22 3a 88 > (actual files are 250MB!) > > decodedTime.txt (should be) > 0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33 > ... > > My boss and I are trying to complete the same task (he figured out how > to do it, but his code uses a while != "" loop and doesn't look > pythony (it looks too 'c'). Not that there's anything wrong with that! > > Any help is really appreciated.
Use the built-in int(). It has an optional argument "radix" which specifies the base for the conversion. For example: >>> int("0x0A", 16) >>> 10 -- http://mail.python.org/mailman/listinfo/python-list