简体   繁体   中英

NameError: variable is not defined when searching for a keyword with python

I am trying to scan multiple files and search for two keywords in the same line. I am trying to look for the keywords "SEQADV" and "MUTATION" in the same line. The problem is I keep getting the error "NameError: name 'wt_residue' is not defined". When I search for one keyword "SEQADV", the program runs smoothly.

        if 'SEQADV' and 'MUTATION' in line:
            try:
                mutation = line.split()
                sequence_number = mutation[4]
                chain = mutation[3]
                mutant_residue = mutation[2]
                wt_residue = mutation[7]
            except IndexError:
                pass

#Prints all data to the .csv file above and closes the file 
            print(",".join([pdb_name, mutant_residue, chain, sequence_number, wt_residue]), file=datafile)    
datafile.close()

Try changing your if statement to if 'SEQADV' in line.split() and 'MUTATION' in line.split():

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM