[Tutor] please help: conditional statement and printing element

Jacob S. keridee at jayco.net
Tue Jan 11 21:32:09 CET 2005


I think I speak for a few of us in the fact that we don't know if that's
close to the correct approach due to the fact that you haven't given us a
full line to look at. Is col[17] the last column? If so, take the code I
just sent and change the line to:

if line.startwith('25\t') and line.endswith('\t1'):

I add the tabs in to make sure that 25 and 1 is the only thing in the
column.
Oh, and add col[9], col[10], etc. to the "\t".join() call. Ooops, that
should also have brackets...

Here it is in full.

file1 = open('psl','r')
for line in file1:
    if line.startswith('25') and line.endswith('1'):
        line = line.split("\t")
        print "\t".join([col[0],col[1],col[9],col[10]])
file1.close()

An alternative that seperates the column indices and the line qualifiers
from the code so they can easily be edited.
Ahh, lets change line to row.

##Beginning of code##
columns_to_print = [0,1,9,10,17]
beginning = '25'
ending = '1'

file1 = open('psl','r')
for row in file1:
    if row.startswith(beginning) and row.endswith(ending):
        row = row.split("\t")
        printlist = [row[x] for x in columns_to_print]
        print "\t".join(printlist)
file1.close()
## Ending of code ##

HTH,
Jacob Schmidt



More information about the Tutor mailing list