iterative_parsing.py 861 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. This code is part of the Udacity quizes throughout the course.
  5. The task is to use the iterative parsing to process the map file and
  6. find out not only what tags are there, but also how many, to get the
  7. feeling on how much of which data you can expect to have in the map.
  8. I fill out the count_tags function. It returns a dictionary with the
  9. tag name as the key and number of times this tag can be encountered in
  10. the map as value.
  11. """
  12. import xml.etree.cElementTree as ET
  13. import pprint
  14. # Change the path with the location of your OSM file
  15. OSMFILE = 'sample.osm'
  16. def count_tags(filename):
  17. tags= {}
  18. for event, elem in ET.iterparse(filename):
  19. if elem.tag not in tags.keys():
  20. tags[elem.tag] = 1
  21. else:
  22. tags[elem.tag] += 1
  23. pprint.pprint(tags)
  24. count_tags(OSMFILE)