[Tutor] please help: conditional statement and printing element

Jacob S. keridee at jayco.net
Tue Jan 11 21:12:33 CET 2005


coll is a string, 25 is an integer.

However, I would do it this way.

file1 = open('psl.txt','r')
for line in file1:
    if line.startwith('25'):
        line = line.split('\t')
        print "\t".join(line[0],line[1],line[17])
file1.close()

This has the added advantage that if you have a big file, it will only take
the time
to split it if it meets the qualifications. i.e. if it starts with '25'
The only way that the advantage would not work is if the string method
startswith
takes more time to run than splitting and searching the first index for a
value. But I
doubt that very much.

HTH,
Jacob Schmidt


> Dear group,
>   For some reason my brain cannot think of any other
> option than what I have in my script. Could any one
> please help me in suggesting.
>
> What I have : (File name : psl)
> 22 2 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
> 22 0 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
> 24 1 457:411 25 0
> 22 0 457:411 25 0
> 21 0 457:411 25 0
> 25 0 457:411 25 0
> 25 0 457:411 25 0
>
>
> What to do:
> I want to print values that are 25 in column 1 and not
> the other values such as 24,22,21 etc.
>
>
> My script:
> >>> for i in range(len(psl)):
> col = split(psl[i],'\t')
> col1 = col[0]
> if col1 == 25:
> print col[0]+'\t'+col[1]+'\t'+col[17]
>
>
> >>>
>
> Result: I get nothing. Am I doing something very
> wrong. Why isnt if col1 == 25: functional.
>
> My idea is to check if col[0] == 25: then print
> columns 1,18 etc.
>
> Can you please help me.
>
> Thanks
> K
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



More information about the Tutor mailing list