I have multiple abstracts in text file format. And I want to fetch disease terms lets say "lung cancer". I need to check whether that text file has this term or not. And I want to use the same code for other terms as well. Please help.
Well that makes things a bit more complex. I guess you'd want something along the lines of this (note: this is assuming that, as above, the PMID ends each abstract):
filename = "abstract_filename_goes_here" # look at argparse to pass the filename from the commandline
outputfile = "output_filename.txt"
search_term = "lung cancer" # again, look into argparse
term_found = False
results = 
f = open(filename, "r")
for line in f:
if search_term in line:
term_found = True
if term_found == True:
results.append(line) # record the PMID line for abstracts containing the search term
term_found = False # reset for the next abstract
out = open(outputfile, "w")
for pmid in results:
Please bear in mind that I haven't tested this, it's not been run at all and I'm far from an expert. You should expect that it will probably not work exactly as is, and you may need to change a few things!
edit: I'd meant to add this as a comment, but clearly clicked the wrong button! Since it kind of is a new answer I guess it can stay here, and I've reached the post limit so I can't move it myself. If any mods feel I've made a mistake though, feel free to sort it out however you see fit.