Python,geopy kill time limit

时间:2017-08-17 13:51:17

标签: python time geopy

我从文件中读取了更多的千位坐标,因为我希望得到相关的国家/地区。我试图杀死时间限制,但它还没有工作,它在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 个答案:

答案 0 :(得分:2)

问题

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

    #!/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()