[Tutor] Files and such 3

Chad Crabtree flaxeater at yahoo.com
Sat Oct 2 04:42:11 CEST 2004


Prospero wrote:

>Greetings!
>
>Again, thanks to Danny. It was at the point of trying to implement a
For loop
>(which you hinted at in your first response) that the whole thing
foundered.  I
>cannot see any way of doing this to make it look at each line in
turn (or at
>least none that have worked so far), let alone doing the same
backwards.
>
>As far as variable names are concerned, agreed, but the stuff I am
doing at the
>moment is for checking out the principles and bears limited
resemblance to the
>intended finished product. Also, I got the impression that line and
lines were
>reserved words.
>
>All the best,
>
>Prospero.
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>
how about this.  Checks everyline of a file in your format to see if
3 
is one of the values

f=open(afile,'r')
for item in f:  #automaticly iterates through every line in a file
    if '3' in item.split(','):
       print "Found"
f.close()


or you could do something really crazy

f=open(afile.'r')
for item in f:
    if 3 in map(int,item.split(',')):
       print found
f.close()

However if you have anything in your file that is not an integer or
can 
be figured out there will be an error.

However this is the same as doing it thus

f=open(afile,'r')
lines=f.readlines()
f.close()
for item in lines:
    if '3' in item.split(',')
       print "Found"

maybe this helps I hope so.

p.s.  Line and lines imho is not a keyword


		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 


More information about the Tutor mailing list