I have written a program that searches through a text file and in the end I want it to give me some probabilities. In my program I'm searching (for each line) for certain characters and if that character exists then extract the probability. The thing is that sometimes this character is not found and the line just gives me DontExist and then I just want a probability to be zero. How can I accomplish this? Where do I say this inside the for loop? My prgram looks like this:
f= open('filename', 'r') data = f.readlines() f.close() f2= open('filename', 'r') dat = f2.readlines() f2.close() for lines in data: if ('##' not in data): start=lines.index("Bgc") end=lines.index(',',"Bgc") prob=[start:end] start2=lines.index("Bgc") end2=lines.index(',',"Bgc") prob2=[start:end] lines = lines.split('\t') r1 = line for line in dat: line = line.split('\t') r2 = line if r2 == r1: return prob/prob2
Where would I put the if loop of Bgc not existing in the file then give back that probability is 0 otherwise divide prob/prob2 and give back this calculated value instead.