Tab expansion problem or ..?

T

Tony C

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'.


So I then tried

fhIn = open("input.txt")

for line in fhIn:
if 'HEXNUM' in line.expandtabs():
Hexloc = line.find('HEXNUM')
print"\n%s" % line[:Hexloc]

and I've tried passing numbers from 1-4 to expantabs, all with
different results.

I've verified that there are tab characters in the input file, by
using an editor which can display the "whitespace characters" as
visible symbols.


Am I overlooking something painfully obvious here ?


thanks in advance
 
P

Peter Otten

Tony said:
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'.


So I then tried

fhIn = open("input.txt")

for line in fhIn:
if 'HEXNUM' in line.expandtabs():
Hexloc = line.find('HEXNUM')
print"\n%s" % line[:Hexloc]

You are throwing away the result of line.expandtabs() in the above code.
Therefore the expansion effects neither the printed line nor the Hexloc
index. It should rather be (untested)

for line in fhIn:
if "HEXNUM" in line:
line = line.expandtabs()
Hexloc = line.find("HEXNUM")
print "\n%s" % line[:Hexloc]
 
T

Tony C

for line in fhIn:
if 'HEXNUM' in line.expandtabs():
Hexloc = line.find('HEXNUM')
print"\n%s" % line[:Hexloc]

You are throwing away the result of line.expandtabs() in the above code.
Therefore the expansion effects neither the printed line nor the Hexloc
index. It should rather be (untested)

for line in fhIn:
if "HEXNUM" in line:
line = line.expandtabs()
Hexloc = line.find("HEXNUM")
print "\n%s" % line[:Hexloc]

In my *actual* code, I am storing the result from expandtabs()
I was away from the computer where my sources were, so I just typed in
a quick
example- but overlooked storeing the result from expantabs().

That being said, I still don't understand why the index returned from
find()does not give me the correct offset into the string, where the
label HeXNUM is.
 
P

Peter Otten

Tony said:
In my *actual* code, I am storing the result from expandtabs()
I was away from the computer where my sources were, so I just typed in
a quick
example- but overlooked storeing the result from expantabs().

That being said, I still don't understand why the index returned from
find()does not give me the correct offset into the string, where the
label HeXNUM is.

I fear that nobody will find the explanation if you don't post the *actual*
code :-(

Peter
 
D

Dave K

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):
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top