jeudi 22 septembre 2016

open conditional python files but read data from one

I want to open and read a file depending on a condition, read only if condition met true. I wrote the following scriptlet:

def bb(fname, species):
     if species in ('yeast', 'sc'):
         pm =  open('file.txt', 'rU')
         for line in pm:
             line = line.split()
             with open(fname, 'rU') as user:
                 for e in user:
                     e = e.split()
                     if e[0] in line:
                         print(line)
    elif species in ('human', 'hs'):
         pm =  open('file2.txt', 'rU')
         for line in pm:
             line = line.split()
             with open(fname, 'rU') as user:
                 for e in user:
                     e = e.split()
                     if e[0] in line:
                         print(line)

Is there a proper pythonic way, where I don't have to repeat/write the same lines (line3 to 10) over and over again ? Thanks !

Aucun commentaire:

Enregistrer un commentaire