473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Tab expansion problem or ..?

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
Jul 18 '05 #1
4 1223
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'.
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]
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


Jul 18 '05 #2
> >
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.
Jul 18 '05 #3
Tony C wrote:
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
Jul 18 '05 #4
On 27 Feb 2004 13:29:13 -0800 in comp.lang.python, ca*******@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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Caleb Hattingh | last post by:
Hi Here is a script I want to be able to write (explanation appears after): *** start of script *** import MyCustomMacroLib # This does the magic I would like help for. # This is not...
4
by: bill | last post by:
Consider the following: import os, commands os.environ="string with foo" a = '$QWE ${QWE/foo/baz}' b = commands.getoutput('echo ' + a) This does what I want, which is to expand a...
7
by: n3crius | last post by:
I've noticed that when I'm programming Windows applications in the .net sutdio I can't see an expansion option in the Solution Explorer to get the .cs behind the WindowsForm. In contrast, I can...
3
by: Ark | last post by:
Hello, NG, Please, help on this snippet: #define CAT(a,b) a##b #define COMMENT CAT(/,/) COMMENT This is a comment Should it compile? It passes MS C/C++ 13.0 (Visual Studio 2002) and fails...
6
by: max(01)* | last post by:
hi. i want to examine preprocessed source which only has certain macros expanded, for example i would like to have: #include <stdio.h> #include "other.c" int main() { ... }
1
by: Robert Phan | last post by:
I was modified the expansions.xml to learn about expansion snippet. Now I got error when trying to invoke the expansion. Error is "No expansion template were found...". Do I needed to reinstall...
1
by: MB2 | last post by:
I have code in a User Control for an image button like this: <asp:ImageButton id="__0" runat="server" text="Delete" ImageUrl="~/Images/ImageLibrary/icon_delete.gif" /> This used to work and it...
4
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.