[Tutor] String question (original) (raw)
orbitz orbitz at ezabel.com
Fri Jul 2 11:50:44 EDT 2004
- Previous message: [Tutor] String question
- Next message: [Tutor] String question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
For starters, you don't need to import string. string.split(line) can be done with line.split(). line is of type str, which is a built in and contains most of the string members. import string is only necessary rarely.
Secondly I could be wrong, but I don't think you have given enough code to really fix the problem. Your error says it's on line 0, but your input() in what you pasted is definitely not. My guess is you have indented someplace you shouldn't, or vice versa, or you have missed a " or something like that.
Good luck
Kooser, Ara S wrote:
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 "", 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 ------------------------------------------------------------------------
Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor
- Previous message: [Tutor] String question
- Next message: [Tutor] String question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]