Search for ways in OpenStreetMap extract
Introduction

Unfortunately imposm is just for Python 2, my project is in Python 3. I think lxml library looks promising. I wrote simple code to test it, right now it is based on just 2 nodes. 

Picture of nodes

I attach picture from XML file so one can see what I am dealing with.



How it works

It is iterating over l_id (which is list of ids) using etree.parse and findall.

First inner loop gathers dictionary where given id is.

Second inner loop gathers dictionary where chosen value is.

Loop for dict_ids_all appends to new list only ids from dictionary.

Loop for dict_ids_all appends to new list only value from dictionary.

My code

tree = lxml.etree.parse(r'path to xml')
l_dict_ids_all = []
l_dict_values_all= []
l_only_id =[]
l_only_values = []
l_id = ['"35121262"', '"35121263"']
name = '"name"'

for id in l_id:
    for tag in tree.findall('//node[@id={0}]'.format(id)):
        l_dict_ids_all.append(tag.attrib)

    for tag in tree.findall('//node[@id={0}]//tag[@k={1}]'.format(id,name)):
        l_dict_values_all.append(tag.attrib)


#printing is only for review purpose

print('Full id dict')        
print(l_dict_ids_all)

print('Full Value dict')
print(l_dict_values_all)

print('Only ID list')
for element in l_dict_ids_all:
    l_only_id.append(element['id'])
print(l_only_id)

print('Only Value list')
for element in l_dict_values_all:
    l_only_values.append(element['k'])
print(l_only_values)


Output


  Full id dict
  
  [{'id': '35121262', 'visible': 'true', 'version': '17',
  'changeset': '41419943', 'timestamp': '2016-08-12T22:24:23Z', 'user':
  'kocio', 'uid': '52087', 'lat': '52.1560439', 'lon': '21.0346808'},
  {'id': '35121263', 'visible': 'true', 'version': '16', 'changeset':
  '41419943', 'timestamp': '2016-08-12T22:24:20Z', 'user': 'kocio',
  'uid': '52087', 'lat': '52.1492285', 'lon': '21.0461042'}]
  
  Full Value dict [{'k': 'name', 'v': 'Stokłosy'}, {'k': 'name', 'v': 'Imielin'}]
  
  Only ID list  ['35121262', '35121263']
  
  Only Value list  ['name', 'name']


What I tried

I am aware that creating list and using it to append items to new list is wrong, but whenever I tried something like this:

l_dict_ids_all.append(tag.attrib[0]['id'])


Received an error :


  TypeError Traceback (most recent call)
  
  ipython-input-91-8b0a49bc5f35 in ()
        7 for id in l_id:
        8     for tag in tree.findall('//node[@id={0}]'.format(id)):
  ----> 9         l_dict_ids_all.append(tag.attrib[0]['id'])
       10
  src/lxml/etree.pyx in lxml.etree._Attrib.getitem()
  
  src/lxml/apihelpers.pxi in lxml.etree._getAttributeValue()
  
  src/lxml/apihelpers.pxi in lxml.etree._getNodeAttributeValue()
  
  src/lxml/apihelpers.pxi in lxml.etree._getNsTag()
  
  src/lxml/apihelpers.pxi in lxml.etree.__getNsTag()
  
  src/lxml/apihelpers.pxi in lxml.etree._utf8()
  
  TypeError: Argument must be bytes or unicode, got 'int'


My goal/problem

Code is working, but I want to make it better.
I need to get rid of 2 out 4 lists which I create at the begging.