Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Wed Sep 24 09:33:04 2014
- @author: vikramk3
- """
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- Your task is to use the iterative parsing to process the map file and
- find out not only what tags are there, but also how many, to get the
- feeling on how much of which data you can expect to have in the map.
- The output should be a dictionary with the tag name as the key
- and number of times this tag can be encountered in the map as value.
- Note that your code will be tested with a different data file than the 'example.osm'
- """
- import xml.etree.ElementTree as ET
- import pprint
- def count_tags(filename):
- # I added elem.clear() after each iteration to avoid any memory issues with the iterative parsing
- total={}
- for event, elem in ET.iterparse(filename, events=("start", "end")):
- if event == "start" and elem.tag not in total:
- total[elem.tag]=1
- elif event== "start" and elem.tag in total:
- total[elem.tag]=total[elem.tag] + 1
- elem.clear()
- return total
- def test():
- tags = count_tags('C:\\Users\\vikramk3\\Documents\\Courses\\Data_Wrangling\\austin_texas.osm')
- pprint.pprint(tags)
- if __name__ == "__main__":
- test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement