[Tutor] Reading lines
Remco Gerlich
scarblac@pino.selwerd.nl
Mon, 21 Aug 2000 20:01:00 +0200
On Mon, Aug 21, 2000 at 01:35:28PM -0400, Daniel D. Laskey, CPA wrote:
> Dear Tutor:
>
> # ---------------------------------------------------
> # import sys and string
> import sys
> import string
>
> # print the following statement
> print "Enter the name of the datafile file to convert:"
>
> # ask the name of the data_file to use
> data_file = raw_input('File name to process? ')
>
> # read the data_file and put it into a file called in_file
> in_file = open(data_file,"r")
>
> # create the file to export the data to
> out_file = open("junk.txt","w+")
>
> # read the file in_file line by line
> lines = in_file.readlines()
# Keep a list of lines you want to keep too:
resultlines = []
> # I want it to search through each line of the file
> # strip out the lines that has 'Total' on it
> # then find each line that has '$' on the line and keep it
> # so I can use it
> i = {}
> for i in range(0,len(lines)):
> string.find('Total')<= 0
> string.find('$')>= 0
# Change this for loop to:
for line in lines:
if string.find(line, 'Total') > -1:
continue # Move on, skip this line
if string.find(line, '$') > -1:
resultlines.append(line) # Keep it
> # # end of processFile() function #
>
> # write the results to the new file
> out_file.writelines(lines)
And write 'resultlines' instead.
> out_file.close()
> in_file.close()
>
> # ---------------------------------------------------
> Traceback (innermost last):
> File "question1.py", line 25, in ?
> string.find('Total')<= 0
> TypeError: function requires at least 2 arguments; 1 given
>
> Are there any examples of how this can be accomplished?
> Why am I having sooooo.. much trouble?
Inside the for loop, you still need to tell Python what to do exactly;
you didn't tell it where to look for 'Total', so it gave an error. You
also didn't do anything with the result.
--
Remco Gerlich, scarblac@pino.selwerd.nl