[Tutor] Reading lines
Denis
spirou@aragne.com
Mon, 21 Aug 2000 20:27:00 +0200
Le Mon, Aug 21, 2000 at 01:35:28PM -0400, Daniel D. Laskey, CPA pianota:
> 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()
>
> # 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 = {}
if you do that, i is a dictionary (?)
I guess you meant
i = []
to make it an empty list, but there is no need to do so.
> for i in range(0,len(lines)):
i will take values from 0 to n-1 where n is the number of lines
> string.find('Total')<= 0
string.find('$')>= 0
You wanted to do something like :
# for every line in list of lines
for line in lines:
#if, searching that line, we don't find 'Total'
if (string.find(line, 'Total') < 0
#and, searching that line, we do find '$'
and string.find(line, '$')>= 0):
# (parentheses let me cut the line where I want)
# then write the line to out_file
out_file.write(line)
> # # end of processFile() function #
if you want a function, you know you have to define it :
def processFile(in, out):
action comes here
> TypeError: function requires at least 2 arguments; 1 given
Pretty explicit ...
print string.find.__doc__
Cordialement'ly y'rs.
--
Denis FRERE
P3B : Club Free-Pytho-Linuxien Caroloregien http://www.p3b.org
Aragne : Internet - Reseaux - Formations http://www.aragne.com