Python, geopy kill 时间限制

Python, geopy kill time limit

我从一个文件中读取了更多的坐标,为此我想得到相关的国家。我试过杀时间限制,但是还不行,在150-160坐标后就停止了。我可以处理这个吗?

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys


with open('alagridsor.txt') as f:
    lines = f.read().splitlines()    


for sor in range(1, 9271):
    print(sor) 

    koor = lines[sor]

    from geopy.geocoders import Nominatim
    from geopy.exc import GeocoderTimedOut

    geolocator = Nominatim()
    location = geolocator.reverse(koor, timeout=None)
    cim = location.raw['address']['country']

    print(cim)

    f = open('out.txt', 'a')
    f.write(cim.encode('utf8'))
    f.write("\n")

问题

  1. 使用f.read() 并省略大小将导致读取并返回文件的全部内容。如果文件是机器内存的两倍,您将遇到问题。
  2. 总是在 for 循环中打开输出文件是非常昂贵的。

可能的解决方案

#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
from geopy.geocoders import Nominatim

geolocator = Nominatim(timeout=None)
fobj_out = open('out.txt', 'a')
with open('alagridsor.txt') as fobj_in:
    for koor in fobj_in:
        location = geolocator.reverse(koor.rstrip())
        cim = location.raw['address']['country']
        fobj_out.write(cim.encode('utf8'))
        fobj_out.write("\n")
        time.sleep(0.5)     # delay 5 milli-seconds between each request
fobj_out.close()