Advertisement
vikramk3

mapparser.py

Oct 15th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Sep 24 09:33:04 2014
  4.  
  5. @author: vikramk3
  6. """
  7.  
  8. #!/usr/bin/env python
  9. # -*- coding: utf-8 -*-
  10. """
  11. Your task is to use the iterative parsing to process the map file and
  12. find out not only what tags are there, but also how many, to get the
  13. feeling on how much of which data you can expect to have in the map.
  14. The output should be a dictionary with the tag name as the key
  15. and number of times this tag can be encountered in the map as value.
  16.  
  17. Note that your code will be tested with a different data file than the 'example.osm'
  18. """
  19. import xml.etree.ElementTree as ET
  20. import pprint
  21.  
  22. def count_tags(filename):
  23.     # I added elem.clear() after each iteration to avoid any memory issues with the iterative parsing
  24.     total={}
  25.     for event, elem in ET.iterparse(filename, events=("start", "end")):
  26.         if event == "start" and elem.tag not in total:
  27.             total[elem.tag]=1
  28.         elif event== "start" and elem.tag in total:
  29.             total[elem.tag]=total[elem.tag] + 1
  30.         elem.clear()
  31.     return total
  32.  
  33.  
  34. def test():
  35.  
  36.     tags = count_tags('C:\\Users\\vikramk3\\Documents\\Courses\\Data_Wrangling\\austin_texas.osm')
  37.     pprint.pprint(tags)
  38.  
  39.  
  40. if __name__ == "__main__":
  41.     test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement