[Tutor] String question

Kooser, Ara S askoose at sandia.gov
Fri Jul 2 11:18:24 EDT 2004


Hello,

    I have posted in here a few times asking questions about data filtering.
I have part of a code set-up for removing header information and then
extracting a column of data (thanks to the python list for help and book
recommendations) then writing the column to another file. My question is
twofold: How do I maintain a columned format when I write the data to a new
file? When I place the data extracting part of my program into the big
filter program I receive the error (I am guessing I have an indent problem
or syntax):

Traceback (most recent call last):
  File "C:\Python23\filter.py", line 49, in ?
    input ()
  File "<string>", line 0  
    ^
SyntaxError: unexpected EOF while parsing.

I have included both the extracting code and the filter code I am trying to
place the extracting code in. Thank you very much. 

EXTRACTING
import string

inp = open("out.txt","r")
outp = open("out2.txt","w")

for line in inp.readlines():
    words = string.split(line)
    if len(words) >= 1: 
        outp.write(words[0]) 

WHOLE FILTER CODE
    def filterFile(infname,outfname):
        inp = open(infname, "r") #opens the file lmps for reading
        outp = open(outfname, "w") #creates the file out.txt for writing
        while 1: #Starts a loop looking for lines with "I" and then writes
out all other lines
            text = inp.readline()
            if text =="":
                break #if there is no text break from the while loop and
continue
            if text[0] =="I": #removes all lines starting with I
                continue
            if text[0] =="0": #removes all lines starting 0
                continue
            outp.write(text) #writes the remaining lines to out.txt
        inp.close() #closes lmps.txt
        outp.close() #closes out.txt
        return 
    filterFile("lmps.txt","out.txt") #calls the function to execute it
    print "Header information has been removed" #tells you that the job has
been completed
    input ()

    import string
    inp = open("out.txt","r")
    outp = open("out2.txt","r")
    for line in inp.readlines():
        words = string.split(line)
        if len(words) >= 1: 
            outp.write.(words[0])

Thanks,
Ara

"There is something to be learned from a rainstorm. When meeting with a
sudden shower, you try not to get wet and run quickly along the road. But
doing such things as passing under the eaves of houses, you still get wet.
When you are resolved from the beginning, you will not be perplexed, though
you still get the same soaking." - Yamamoto Tsunetomo

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040702/11d1703c/attachment-0001.html


More information about the Tutor mailing list