Tab expansion problem or ..?

Dave K dk123456789 at REMOVEhotmail.com
Sun Feb 29 14:52:05 EST 2004


On 27 Feb 2004 13:29:13 -0800 in comp.lang.python, cappy2112 at yahoo.com
(Tony C) wrote:

>I've written a program which reads a text file in , and writes it out
>to a new filename.
>
>When the program encounters a line which contains a specific text
>label-, I read in the hex number adjacent to the label, convert the
>number to decimal, then write the line of text to the output file.
>
>Essentially, the output file is mostly identical to the input file,
>with the exception of the hex numbers being converted to decimal.
>
>The problem I'm having is getting the exact location of the hex
>numbers.
>
>fhIn = open("input.txt")
>
>for line in fhIn:
>  if 'HEXNUM' in line:
>    Hexloc = line.find('HEXNUM')
>    print"\n%s" % line[:Hexloc]
>
>The index returned by find() does not seem to be accurate.
>
>When I display the line using the offset (Hexloc), the text displayed
>is many characters away from the text label 'HEXNUM'.


It works perfectly well for me, with and without tabs (I've changed
the print statement slightly to include the search text):

>>> def find_hexnum(fhIn):
	    for line in fhIn:
		        if 'HEXNUM' in line:
			            Hexloc = line.find('HEXNUM')
			            print"%s" % line[:Hexloc+6]

>>> find_hexnum(['123HEXNUMaa456789','abcHEXNUM00defg'])
123HEXNUM
abcHEXNUM
>>> find_hexnum(['12\t3HEXNUMaa456789','abc\tHEXNUM00defg'])
12	3HEXNUM
abc	HEXNUM

If this doesn't work for you, a sample line from your input file which
is incorrectly parsed, plus your code which does the parsing, would be
useful.

Dave




More information about the Python-list mailing list