0

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 ?

jax
  • 3,927
  • 7
  • 41
  • 70
  • Possible duplicate of [Using OR comparisons with IF statements](http://stackoverflow.com/questions/148042/using-or-comparisons-with-if-statements) – Son of a Beach May 11 '17 at 05:52

4 Answers4

3

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.

Ian
  • 30,182
  • 19
  • 69
  • 107
0

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.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
0

Instead of

if  "<AbstractText>" or "<Title>" in line:

use

if  "<AbstractText>" in line or "<Title>" in line:
MarianD
  • 13,096
  • 12
  • 42
  • 54
0

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")
FrostyOnion
  • 856
  • 7
  • 10