0
import csv
from geopy import geocoders
import time

g = geocoders.GeocoderDotUS()

spamReader = csv.reader(open('locations.csv', 'rb'), delimiter='\t', quotechar='|')

f = open("output.txt",'w')

for row in spamReader:
    a = ', '.join(row)
    #exactly_one = False
    time.sleep(1)


    place, (lat, lng) = g.geocode(a)


    if None in (lat, lng):
        f.write("none")
    else:
        b = str(place) + "," + "[" + str(lat) + "," + str(lng) + "]" + "\n"
        print b
        f.write(b)

所以我已经确定,如果 GeocoderDotUS 没有找到地址,它将返回 None。我已经编写了一些脚本来尝试检查 None 但是我似乎仍然得到了这个跟踪。我有点困惑。

Traceback (most recent call last):
File "C:\Users\Penguin\workspace\geocode-nojansdatabase\src\GeocoderDotUS.py", line 17, in <module>
place, (lat, lng) = g.geocode(a)
TypeError: 'NoneType' object is not iterable

我对 None systax 的检查是否有一些错误?提前感谢您的任何帮助....

4

2 回答 2

1

正如您在错误消息中看到的那样,问题出在该place, (lat, lng) = ...行中。

g.geocode calll返回None(并且您尝试立即将其分配给place变量和lat, lng元组,这显然必须失败)。

因此,请尝试以下方法:

result = g.geocode(a)
if result:
    place, (lat, lng) = result
else:
    # ...
于 2012-06-02T20:20:46.103 回答
1

I don't know anything about geopy, but it looks like the problem is just what you said: if geocode doesn't find it, it returns None. Just None, not a tuple with None as the elements. So you need to check for None before you assign the results to lat and lng. Something like:

geoResult = g.geocode(a)
if geoResult is None:
    f.write("none")

# If we get here, the result is not None and we can proceed normally
place, (lat, lng) = geoResult
b = "..." # continue with your processing here
于 2012-06-02T20:22:50.490 回答