Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2.  
  3. article_file = "exampleResearchArticle.xml"
  4.  
  5.  
  6. def get_root(fname):
  7.     tree = ET.parse(fname)
  8.     return tree.getroot()
  9.  
  10.  
  11. def get_authors(root):
  12.     authors = []
  13.     for author in root.findall('./fm/bibl/aug/au'):
  14.         data = {
  15.                 "fnm": None,
  16.                 "snm": None,
  17.                 "email": None
  18.         }
  19.  
  20.         data["fnm"] = author.find('./fnm').text
  21.         data["snm"] = author.find('./snm').text
  22.         data["email"] = author.find('./email').text
  23.  
  24.         authors.append(data)
  25.  
  26.     return authors
  27.  
  28.  
  29. def test():
  30.     solution = [{'fnm': 'Omer', 'snm': 'Mei-Dan', 'email': 'omer@extremegate.com'}, {'fnm': 'Mike', 'snm': 'Carmont', 'email': 'mcarmont@hotmail.com'}, {'fnm': 'Lior', 'snm': 'Laver', 'email': 'laver17@gmail.com'}, {'fnm': 'Meir', 'snm': 'Nyska', 'email': 'nyska@internet-zahav.net'}, {'fnm': 'Hagay', 'snm': 'Kammar', 'email': 'kammarh@gmail.com'}, {'fnm': 'Gideon', 'snm': 'Mann', 'email': 'gideon.mann.md@gmail.com'}, {'fnm': 'Barnaby', 'snm': 'Clarck', 'email': 'barns.nz@gmail.com'}, {'fnm': 'Eugene', 'snm': 'Kots', 'email': 'eukots@gmail.com'}]
  31.    
  32.     root = get_root(article_file)
  33.     data = get_authors(root)
  34.  
  35.     assert data[0] == solution[0]
  36.     assert data[1]["fnm"] == solution[1]["fnm"]
  37.  
  38.  
  39. test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement