Extract data from ASCII file

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Sun Feb 22 11:09:31 EST 2004


Ren wrote:

> Suppose I have a file containing several lines similar to this:
> 
> :10000000E7280530AC00A530AD00AD0B0528AC0BE2

Say the file is called data.txt
Try this:
---------------------------------
def process(line):
	line=line[9:]
	result=[]
	for i in range(0,32,4):
		result.append( line[i+2:i+4] + line[i:i+2] )
	return result

for line in open("data.txt"):
	print process(line)
---------------------------------
For your single example data line, it prints
['28E7', '3005', '00AC', '30A5', '00AD', '0BAD', '2805', '0BAC']

It's a list containing the 8 extracted hexadecimal strings.
Instead of printing the list you can do whatever you want with it.
If you need more info, just ask.

--Irmen de Jong



More information about the Python-list mailing list