Friday, 1 January 2016

XML : Fetching XML attributes in Python?

For example i have the following XML file: In each phrase, I always have 2 tags and "x" attribute is either PERS, LOC or ORG. What I want to do is get all pairs in which they are ORG & PERS only

XML Sample:

  <PHRASE>  <N y='0'> back</N>  <en x='PERS'>John</en>  <PREP>to</PREP>  <en x='LOC'>New York</en>  </PHRASE>    

I am using element tree, I tried that, but it retrieves any PERS or ORG no matter what is the second "en" tag. And I only want PERS and ORG pairs (i.e.: when they appear in the same phrase)

  for en in root.findall('./PHRASE/en'):      NE = en.get('x')      if(NE) == "ORG":       print("ORG is: ",en.text)      NE2=en.get('x')      if(NE2) == "PERS":          print("PER is:", en.text)    

No comments:

Post a Comment