Extract data from ASCII file

eleyg eley_greg at bah.com
Mon Feb 23 04:50:45 CET 2004

rlpreston at sbec.com (Ren) wrote in message news:<3602cff7.0402220711.3f7ed46 at posting.google.com>...
> Suppose I have a file containing several lines similar to this:
> :10000000E7280530AC00A530AD00AD0B0528AC0BE2
> The data I want to extract are 8 hexadecimal strings, the first of
> which is E728, like this:
> :10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2
> Also, the bytes in the string are reversed. The E728 needs to be 28E7,
> 0530 needs to be 3005 and so on.
> I can do this in C++ and Pascal, but it seems like Python may be more
> suited for the task.
> How is this accomplished using Python?

The first response only works with python-2.3 (yield is a newly
reserved word).

The second response did not work for me and left off the last couple

You might want to try this. It iterates down the list, grabbing two
characters at a time, reversing them and appending them to a list. It
also allows a second list argument to store the first 8 digits
(mutable lists are passed by reference)

from types import *

def process(line,key):
	""" Pass in a string type (line) and 
		an empty list to store the key """
	if type(key) is ListType and key == []:
		print "Key not ListType or not empty"
	while line:
		k2,k1 = line[:2],line[2:4]
	return result

More information about the Python-list mailing list