Hi, I am trying to audit the open street map data which is a XML file. In this i am trying to find the street name which are not in the correct format. But when i am running i am getting the "IndexError: pop from empty stack". Please help me the find where i am wrong. Please find below the code and the error.
Code :
import xml.etree.cElementTree as ET
from collections import defaultdict
import re
import pprint
regex = re.compile(r'\b\S+\.?', re.IGNORECASE)
expected = ["Ahmedabad", "Road", "NR", "Avenue", "SBK", "Gandhi", "Bridge", "Society"] #expected names in the dataset
# Search string for the regex. If it is matched and not in the expected list then add this as a key to the set.
def audit_street(street_types, street_name):
m = regex.search(street_name)
if m:
street_type = m.group()
if street_type not in expected:
street_types[street_type].add(street_name)
def is_street_name(elem): # Check if it is a street name
return (elem.attrib['k'] == "addr:street")
def main(): # return the list that satify the above two functions
osm_file = open("D:/Udacity/ud120-projects-master/new-delhi_india.osm", "r")
street_types = defaultdict(set)
for event, elem in ET.iterparse(osm_file, events=("start",)):
if elem.tag == "node" or elem.tag == "way":
for tag in elem.iter("tag"):
if is_street_name(tag):
audit_street(street_types, tag.attrib['v'])
osm_file.close()
print street_types
if __name__== "__main__":
main()
Error:
Traceback (most recent call last):
File "C:\Users\prateek\Desktop\xx.py", line 39, in <module>
main()
File "C:\Users\prateek\Desktop\xx.py", line 27, in main
for event, elem in ET.iterparse(osm_file, events=("start",)):
File "<string>", line 103, in next
IndexError: pop from empty stack
Code :
import xml.etree.cElementTree as ET
from collections import defaultdict
import re
import pprint
regex = re.compile(r'\b\S+\.?', re.IGNORECASE)
expected = ["Ahmedabad", "Road", "NR", "Avenue", "SBK", "Gandhi", "Bridge", "Society"] #expected names in the dataset
# Search string for the regex. If it is matched and not in the expected list then add this as a key to the set.
def audit_street(street_types, street_name):
m = regex.search(street_name)
if m:
street_type = m.group()
if street_type not in expected:
street_types[street_type].add(street_name)
def is_street_name(elem): # Check if it is a street name
return (elem.attrib['k'] == "addr:street")
def main(): # return the list that satify the above two functions
osm_file = open("D:/Udacity/ud120-projects-master/new-delhi_india.osm", "r")
street_types = defaultdict(set)
for event, elem in ET.iterparse(osm_file, events=("start",)):
if elem.tag == "node" or elem.tag == "way":
for tag in elem.iter("tag"):
if is_street_name(tag):
audit_street(street_types, tag.attrib['v'])
osm_file.close()
print street_types
if __name__== "__main__":
main()
Error:
Traceback (most recent call last):
File "C:\Users\prateek\Desktop\xx.py", line 39, in <module>
main()
File "C:\Users\prateek\Desktop\xx.py", line 27, in main
for event, elem in ET.iterparse(osm_file, events=("start",)):
File "<string>", line 103, in next
IndexError: pop from empty stack