6

I'm trying to get response from nominatim to geo-code few thousands of cities.

import os
import requests
import xml.etree.ElementTree as ET

txt = open('input.txt', 'r').readlines()
for line in txt:
 lp, region, district, municipality, city = line.split('\t')
 baseUrl = 'http://nominatim.openstreetmap.org/search/gb/'+region+'/'+district+'/'+municipality+'/'+city+'/?format=xml' 
 # eg. http://nominatim.openstreetmap.org/search/pl/podkarpackie/stalowowolski/Bojan%C3%B3w/Zapu%C5%9Bcie/?format=xml
 resp = requests.get(baseUrl)
 resp.encoding = 'UTF-8' # special diacritics
 msg = resp.text
 # parse response to get lat & long
 tree = ET.parse(msg)
 root = tree.getroot()
 print tree

but the result is:

Traceback (most recent call last):
File "geo_miasta.py", line 17, in <module>
    tree = ET.parse(msg)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 647, in parse
    source = open(source, "rb")    
IOError: [Errno 2] No such file or directory: u'<?xml version="1.0" encoding="UTF-8" ?>\n<searchresults timestamp=\'Tue, 11 Feb 14 21:13:50 +0000\' attribution=\'Data \xa9 OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright\' querystring=\'\u015awierczyna, Drzewica, opoczy\u0144ski, \u0142\xf3dzkie, gb\' polygon=\'false\' more_url=\'http://nominatim.openstreetmap.org/search?format=xml&amp;exclude_place_ids=&amp;q=%C5%9Awierczyna%2C+Drzewica%2C+opoczy%C5%84ski%2C+%C5%82%C3%B3dzkie%2C+gb\'>\n</searchresults>'

What is wrong with this?

Edit: Thant to @rob my solution is:

#! /usr/bin/env python2.7
# -*- coding: utf-8 -*-

import os
import requests
import xml.etree.ElementTree as ET

txt = open('input.txt', 'r').read().split('\n')

for line in txt:
    lp, region, district, municipality, city = line.split('\t')
    baseUrl = 'http://nominatim.openstreetmap.org/search/pl/'+region+'/'+district+'/'+municipality+'/'+city+'/?format=xml'
    resp = requests.get(baseUrl)
    msg = resp.content
    tree = ET.fromstring(msg)
    for place in tree.findall('place'):
    location = '{:5f}\t{:5f}'.format(
        float(place.get('lat')),
        float(place.get('lon')))

    f = open('result.txt', 'a')
    f.write(location+'\t'+region+'\t'+district+'\t'+municipality+'\t'+city)
    f.close()
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
m93
  • 113
  • 1
  • 8

2 Answers2

7

You are using xml.etree.ElementTree.parse(), which takes a filename or a file object as an argument. But, you are not passing a file or file object in, you are passing a unicode string.

Try xml.etree.ElementTree.fromstring(text).

Like this:

 tree = ET.fromstring(msg)

Here is a complete sample program:

import os
import requests
import xml.etree.ElementTree as ET

baseUrl = 'http://nominatim.openstreetmap.org/search/pl/podkarpackie/stalowowolski/Bojan%C3%B3w/Zapu%C5%9Bcie\n/?format=xml'
resp = requests.get(baseUrl)
msg = resp.content
tree = ET.fromstring(msg)
for place in tree.findall('place'):
  print u'{:s}: {:+.2f}, {:+.2f}'.format(
    place.get('display_name'),
    float(place.get('lon')),
    float(place.get('lat'))).encode('utf-8')
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thanks, this move the error border to encoding space: ` UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 115: ordinal not in range(128) ` – m93 Feb 12 '14 at 08:34
  • @m93 - That is because you are using `resp.text` instead of `resp.content`. See my edit for a complete program that should get you started. – Robᵩ Feb 12 '14 at 15:26
0
import os,sys,time
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import parse
tree = ET.parse('D:\Reddy\BankLoanAcctService_transactionInq.xml')
root=tree.getroot()

for TrxnEffDt in root.iter('TrxnEffDt'):
 new_TrxnEffDt= str(time.strftime("%y-%m-%d"))
 TrxnEffDt=str(new_TrxnEffDt)

filename2 ="D:\Reddy\BankLoanAcctService_transactionInq2.txt"
r=open(filename2,'w')
sys.stdout =r
  • Traceback (most recent call last): File "D:\Reddy\Python\new.py", line 4, in tree = ET.parse('D:\Reddy\BankLoanAcctService_transactionInq.xml') File "C:\Python33\lib\xml\etree\ElementTree.py", line 1242, in parse tree.parse(source, parser) File "C:\Python33\lib\xml\etree\ElementTree.py", line 1730, in parse self._root = parser._parse(source) File "", line None xml.etree.ElementTree.ParseError: syntax error: line 1, column 0 – user5493252 Oct 27 '15 at 10:57
  • This is the error message i am getting. pls help me – user5493252 Oct 27 '15 at 10:58
  • You should probably ask your own question instead of using an answer on this topic (to avoid confusion and be sure to get a solution to your specific problem), and, if you think the problem is related, you should put a link to this one to help answerers. – Tiesselune Oct 27 '15 at 11:07