Reputation: 4197
I have write down a code to fetch scientific literature, given code bellow fetch "xml" file and extract desired data or lines from the large file.
from this file i want two lines to be printed that contains tag "AbstractText" or "Title", when I use either "AbstractText" or "Title" with "if" it prints desirable out put but when i used both the tags with "or" statement code starts to print all the lines.
Code which is not running correctly:
def fetch_abstract(pmid):
handle = efetch(db='pubmed', id=pmid, retmode='xml')
lines = handle.readlines()
for line in lines:
if "<AbstractText>" or "<Title>" in line:
print line,
fetch_abstract("19555725")
Code is running correctly with "AbstractText" tag :
def fetch_abstract(pmid):
handle = efetch(db='pubmed', id=pmid, retmode='xml')
lines = handle.readlines()
for line in lines:
if "<AbstractText>" in line:
print line,
fetch_abstract("19555725")
Code is running correctly with "Title" tag:
def fetch_abstract(pmid):
handle = efetch(db='pubmed', id=pmid, retmode='xml')
lines = handle.readlines()
for line in lines:
if "<Title>" in line:
print line,
fetch_abstract("19555725")
how can i solve this problem ?
Upvotes: 0
Views: 101
Reputation: 996
EDIT: Fixed syntax error Try this like so:
def fetch_abstract(pmid):
handle = efetch(db='pubmed', id=pmid, retmode='xml')
lines = handle.readlines()
for line in lines:
if "<AbstractText>" in line or "<Title>" in line:
print line,
fetch_abstract("19555725")
Upvotes: 0
Reputation: 30813
You should put your condition with in line
for both:
if "<AbstractText>" in line or "<Title>" in line:
The way you put right now has two following conditions:
"<AbstractText>" or
"<Title>" in line
and "<AbstractText>"
is always true
since the string
"<AbstractText>"
contains something (if "nonemptystring"
is always true
). That is why you print everything.
Upvotes: 3
Reputation: 14121
Instead of
if "<AbstractText>" or "<Title>" in line:
use
if "<AbstractText>" in line or "<Title>" in line:
Upvotes: 0
Reputation: 40516
One correct way is to write:
if "<AbstractText>" in line or "<Title>" in line:
Your first attempt is equivalent to if ("<AbstractText>") or ("<Title>" in line):
. I added the parentheses to emphasize how the line is interpreted.
Upvotes: 0