为什么我的geopy循环总是以9结尾?

2024-05-13 13:08:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个地址列表,当我尝试添加坐标时,我只会得到一个kill9错误。你知道吗

是不是超时了?我增加了睡眠时间来防止它。你知道吗

我得到这个错误Killed: 9

def do_geocode(Nominatim, address):
    time.sleep(3)
    try:
        return Nominatim.geocode(address)
    except GeocoderTimedOut:
        return do_geocode(Nominatim,address)

def addCoordinates(businessList):
    businessList[0] = ["pageNum","entryNum","name","address","tagOne","tagTwo","tagThree","geoAddress","appendedLocation","latitude","longitude","key"]

    geolocator = Nominatim(timeout=None)
    z = 0
    i=1
    while i < len(businessList):
        longitude = ""
        latitude = ""
        geoLocation = ""
        geoAddress = ""
        entry = []

        appendedLocation = (businessList[i][3] + ", San Francisco")

        geoLocation = do_geocode(geolocator, appendedLocation)

        if geoLocation is not None:
            geoAddress = geoLocation.address
            latitude = geoLocation.latitude
            longitude = geoLocation.longitude

            entry = [geoAddress, appendedLocation, str(latitude), str(longitude)]
            j=0
            while j < len(entry):
                businessList[i] += [entry[j]]
                j+=1
            print("coordinates added")
            z +=1
            print(z)

        i+=1

Tags: returnaddressdef错误dogeocodeentrylatitude
1条回答
网友
1楼 · 发布于 2024-05-13 13:08:30

Killed: 9可能意味着您的Python脚本已被操作系统中的某个东西终止(可能是OOM killer?)。确保脚本不会占用计算机的全部可用内存。你知道吗

对于geopy,我建议看一下RateLimiter类。还要注意,在使用namignim时,需要指定唯一的用户代理(这在the Nominatim class docs中进行了解释)。你会得到这样的结果:

from geopy.extra.rate_limiter import RateLimiter


def addCoordinates(businessList):
    businessList[0] = ["pageNum","entryNum","name","address","tagOne","tagTwo","tagThree","geoAddress","appendedLocation","latitude","longitude","key"]

    geolocator = Nominatim(user_agent="specify_your_app_name_here", timeout=20)
    geocode = RateLimiter(
        geolocator.geocode, 
        min_delay_seconds=3.0,
        error_wait_seconds=3.0,
        swallow_exceptions=False, 
        max_retries=10,
    )
    z = 0
    i=1
    while i < len(businessList):
        longitude = ""
        latitude = ""
        geoLocation = ""
        geoAddress = ""
        entry = []

        appendedLocation = (businessList[i][3] + ", San Francisco")

        geoLocation = geocode(appendedLocation)

        if geoLocation is not None:
            geoAddress = geoLocation.address
            latitude = geoLocation.latitude
            longitude = geoLocation.longitude

            entry = [geoAddress, appendedLocation, str(latitude), str(longitude)]
            j=0
            while j < len(entry):
                businessList[i] += [entry[j]]
                j+=1
            print("coordinates added")
            z +=1
            print(z)

        i+=1

相关问题 更多 >