Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How do I get coordinates for addresses from the csv list?
#1
I have a list with addresses in the csv file. I would like to get coordinates for this whole list. Unfortunately, I'm getting an error:

Traceback (most recent call last):

      File "<ipython-input-375-e8f87c93126d>", line 8, in <module>
        print(location.address)

    AttributeError: 'NoneType' object has no attribute 'address'
I do not know what is wrong. When I enter instead of and a specific address from my list, the loop works correctly.

from geopy.geocoders import Nominatim

for i in adres:
    geolocator = Nominatim()
    location = geolocator.geocode(i)
    time.sleep(.45)
    print(location.address)
    print((location.latitude, location.longitude))
This is a part of my list:

print(adres)
['Ul. Sądowa 8 74-320 Barlinek', 'Ul. Armii Polskiej 36 73-260 Pełczyce'}]
Can anyone tell me what is wrong?
Quote
#2
can you print i, just before line 5? by the way i is terrible name in this case. I would use something more meaningful
Quote
#3
OK. I will change it.
I added print(i) before 5 line. This is result:

runfile('/untitled0.py', wdir='/')
Ul. Sądowa 8 74-320 Barlinek
Traceback (most recent call last):

  File "<ipython-input-446-035effc48ddb>", line 1, in <module>
    runfile('/untitled0.py', wdir='/')

  File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/untitled0.py", line 6, in <module>
    print(location.address)

AttributeError: 'NoneType' object has no attribute 'address'
Quote
#4
You most check vaule geolocator.geocode(i).
If i is not found location will return None.
geolocator = Nominatim() can be set before you loop.
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("oslo")
>>> location
Location(Oslo, 26, Norge, (59.9132694, 10.7391112, 0.0))
>>> 
>>> location.address
'Oslo, 26, Norge'

>>> # If give a wrong adress
>>> location = geolocator.geocode("___")
>>> repr(location)
'None'
>>> location.address
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'address'
Quote
#5
Thank you. You probably are right.
Do you have an idea how to solve it? Should I prepare a list of addresses in a different way? How I tried, for example, this:

import geocoder
g = geocoder.google('Ul. Sądowa 8 74-320 Barlinek')
g.latlng
It works!
Out[476]: [52.9914998, 15.2167944]
but when I try to loop with the same addresses:

import geocoder

lat_lng = []
for address in adres:
    g = geocoder.google(address)
    g.latlng
    print(address)
    print(lat_lng)
    lat_lng.append((lat_lng))

print (lat_lng)
Ul. Sądowa 8 74-320 Barlinek
[]
Ul. Armii Polskiej 36 73-260 Pełczyce
[[...]]
Ul. H. Sienkiewicza 5 66-500 Strzelce Krajeńskie
[[...], [...]]
Ul. Wojska Polskiego 15 78-200 Białogard
[[...], [...], [...]]
Ul. Zwycięstwa 74 75-042 Koszalin
[[...], [...], [...], [...]]
Ul. Jana z Kolna 11 75-204 Koszalin
[[...], [...], [...], [...], [...]]
Ul. Mieszka I 11a 74-500 Chojna
[[...], [...], [...], [...], [...], [...]]
Ul. Nowe Czarnowo 76 74-105 Nowe Czarnowo
[[...], [...], [...], [...], [...], [...], [...]]
Ul. kard. S. Wyszyńskiego 2 74-300 Myślibórz
[[...], [...], [...], [...], [...], [...], [...], [...]]
Ul. Wolności 16 ( obok sądu) 73-200 Choszczno
[[...], [...], [...], [...], [...], [...], [...], [...], [...]]
Ul. Bolesława Chrobrego 1 73-200 Choszczno
[[...], [...], [...], [...], [...], [...], [...], [...], [...], [...]]
Ul. Rynek 4/5 78-550 Czaplinek
[[...], [...], [...], [...], [...], [...], [...], [...], [...], [...], [...]]
Quote
#6
it should be (line#9):
lat_lng.append(g.latlng)
Quote
#7
Thank you. It is working now.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Raycasting GPS coordinates drybulkfreight 10 516 Aug-18-2019, 02:54 PM
Last Post: drybulkfreight
  Read list of IP addresses from file and return a network dflick 4 738 Oct-27-2018, 09:33 AM
Last Post: buran
  Help required to print MAC addresses anna 50 6,146 Feb-22-2018, 09:53 AM
Last Post: anna
  How to get data instead of memory addresses KBingo 3 2,065 Jun-12-2017, 12:36 AM
Last Post: KBingo
  Getting remote addresses from database desudesu 3 1,513 May-12-2017, 08:45 AM
Last Post: desudesu

Forum Jump:


Users browsing this thread: 1 Guest(s)