vinaynischal14k

extract countries using python

Jan 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2. import re
  3.  
  4. flag=0
  5.  
  6. counttree = ET.parse('countries.xml')
  7. countroot = counttree.getroot()
  8.  
  9. str = "Department of Neurosurgery, University of Southern California,."
  10.  
  11. for tokens in re.split('; |, |\.|\n',str):
  12.     for country in countroot.findall('Country[name="'+tokens+'"]') + countroot.findall('Country[code="'+tokens+'"]'):
  13.         cname = country.find('name').text
  14.         flag=1
  15.        
  16. if(flag==1):
  17.     print("<Country>"+cname+"</Country>")
  18. else:
  19.     print("<Country>-</Country>")
Add Comment
Please, Sign In to add comment