Filter
Tim Hochberg
tim.hochberg at ieee.org
Tue Apr 11 01:43:41 EDT 2000
"Matthew" <shayman at uniserve.com> wrote in message
news:955427129.619305 at neptune.uniserve.ca...
> I need to write a filter that inserts tabs between Log values . So that
> lines 1 to 3 come out looking something like lines 4 to 6.
> I can then open it in Excel as a dat file with the values in different
> cells, where i can then use formulas and macros.
> The first 9 lines are constants in the header ,but there could be serveral
> headers with data in the whole file .
>
> I'm totaly at square one here.
> I have to figure out how to open the file for modification - as a string ?
> write a for loop for each line beginning with L11 ?
> And a function to insert the white space's between the values.
>
>
> I imagine this to be an ideal task for a small script , but untill i get
> there any advice or help appreciated.
Maybe the following (untested, use at your own risk, etc) code will get you
started and help you refine your question a bit. You will probably wish to
improve it at least some -- for instance I think a blank line (bare newline)
would cause this to hang.
import string
def tabbify(infile, outfile):
# Read and write the version line
outfile.write(infile.readline())
# Read first line
line = infile.readline()
# Loop till we run out of data
while line != "": # No more data to be read (blank lines are "\n"):
# Write 8 header lines unchanged
for i in range(8):
outfile.write(infile.readline())
# Now read lines, converting spaces to tabs till more header lines
are reached
while 1: # loop forever
line = infile.readline()
if line == "" or line[0] == "H": # File's empty or we have a new
set of headers
break
line = string.replace(line, " ", "\t") # replace spaces with
tabs
outfile.write(line)
infile = open("somefilename")
outfile = open("someotherfilename", 'w')
tabbify(infile, outfile)
Enjoy,
-tim
> VER1.5A
> "H0:1,45"
> "H1:9085,*.* "
> "H2:71G,NORTHVIEW DLS, ,MITSUI"
> "H3:VANCOUVER,CHILLIWACK"
> "H4:00100S,100,SB4,01, , , "
> "H5:000330, AH00-93-,SPRUCE, ,N"
> "H6:45,126825"
> H7:220000
> L11SSSP 503438G 1627 # 1
> L11SSSP 563338G 1826
> L11SSSP 573036G 1924
> L11 SS SP 55 36 40 G 18 28 # 4
> L11 SS SP 51 40 46 G 17 31
> L11 SS SP 57 30 37 H 19 24
> etcXXXXXXXXXXXXXXXXXXXXXXX
> "H9:1,45"
>
>
>
More information about the Python-list
mailing list