Extract data from ASCII file

Mike C. Fletcher mcfletch at rogers.com
Sun Feb 22 10:37:16 EST 2004


With Python 2.3:

 >>> def splitter( line ):
...     line = line[9:] # skip prefix
...     while line:
...         prefix, line = line[:4],line[4:]
...         yield prefix[2:]+prefix[:2]
...
 >>> for number in splitter( ':10000000E7280530AC00A530AD00AD0B0528AC0BE2'):
...     print number
...
28E7
3005
00AC
30A5
00AD
0BAD
2805
0BAC
E2
 >>>

If you want to convert the hexadecimal strings to actual integers, use 
int( prefix, 16 ).

HTH,
Mike

Ren wrote:

>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?
>  
>
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/






More information about the Python-list mailing list