Advertisement
Guest User

Untitled

a guest
Oct 29th, 2017
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Your task here is to extract data from xml on authors of an article
  3. # and add it to a list, one item for an author.
  4. # See the provided data structure for the expected format.
  5. # The tags for first name, surname and email should map directly
  6. # to the dictionary keys, but you have to extract the attributes from the "insr" tag
  7. # and add them to the list for the dictionary key "insr"
  8. import xml.etree.ElementTree as ET
  9.  
  10. article_file = "exampleResearchArticle.xml"
  11.  
  12.  
  13. def get_root(fname):
  14.     tree = ET.parse(fname)
  15.     return tree.getroot()
  16.  
  17.  
  18. def get_authors(root):
  19.     authors = []
  20.     for author in root.findall('./fm/bibl/aug/au'):
  21.         data = {
  22.                 "fnm": None,
  23.                 "snm": None,
  24.                 "email": None,
  25.                 "insr": []
  26.         }
  27.  
  28.         # YOUR CODE HERE
  29.         data['fnm']= author.find('fnm').text
  30.         data['snm']= author.find('snm').text
  31.         data['email']= author.find('email').text
  32.        
  33.         data['insr']=author.find('insr').attrib('iid')
  34.        
  35.        
  36.         authors.append(data)
  37.  
  38.     return authors
  39.  
  40.  
  41. def test():
  42.     solution = [{'insr': ['I1'], 'fnm': 'Omer', 'snm': 'Mei-Dan', 'email': 'omer@extremegate.com'},
  43.                 {'insr': ['I2'], 'fnm': 'Mike', 'snm': 'Carmont', 'email': 'mcarmont@hotmail.com'},
  44.                 {'insr': ['I3', 'I4'], 'fnm': 'Lior', 'snm': 'Laver', 'email': 'laver17@gmail.com'},
  45.                 {'insr': ['I3'], 'fnm': 'Meir', 'snm': 'Nyska', 'email': 'nyska@internet-zahav.net'},
  46.                 {'insr': ['I8'], 'fnm': 'Hagay', 'snm': 'Kammar', 'email': 'kammarh@gmail.com'},
  47.                 {'insr': ['I3', 'I5'], 'fnm': 'Gideon', 'snm': 'Mann', 'email': 'gideon.mann.md@gmail.com'},
  48.                 {'insr': ['I6'], 'fnm': 'Barnaby', 'snm': 'Clarck', 'email': 'barns.nz@gmail.com'},
  49.                 {'insr': ['I7'], 'fnm': 'Eugene', 'snm': 'Kots', 'email': 'eukots@gmail.com'}]
  50.  
  51.     root = get_root(article_file)
  52.     data = get_authors(root)
  53.  
  54.     assert data[0] == solution[0]
  55.     assert data[1]["insr"] == solution[1]["insr"]
  56.  
  57.  
  58. test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement