我有一个问题,我不知道如何解决。我想迭代一个文件,在其中将坐标转换为地理位置地址。代码工作正常,但在迭代文件中的一定数量的行后,就会出现问题。
from __future__ import print_function
from geopy.geocoders import Nominatim
from shapely.wkt import loads as load_wkt
from shapely.geometry import Point, Polygon
import io
import re
import ast
import time
geolocator = Nominatim()
with io.open('sample_test2.txt', encoding="utf-8") as f, io.open('sample_test3.txt', 'w',encoding="utf-8") as g:
for line in f:
m = re.sub(r'(70[0-9]+,).*', r'\1', line.rstrip())
z = re.sub(r'.*POINT \([0-9]+.[0-9]+ -[0-9]+.[0-9]+\)(.*)', r'\1', line.rstrip())
c = re.sub(r'.*POINT \(([0-9]+.[0-9]+) (-[0-9]+.[0-9]+)\).*', r'"\1, \2"', line.rstrip())
k = ast.literal_eval(c)
location = geolocator.reverse(k, timeout=60)
h = location.address
j = re.sub(r'.*, ([^,]+, [^,]+), [0-9]+, United.*', r'\1', h.rstrip())
print (m, j, z, file = g)
f.close()
g.close()
现在我读了一些我应该使用的其他问题time.sleep()
。现在我想把它放在print
。我第一次运行我的代码(没有time.sleep()
)在出现此错误之前,他转换了大约 1800 行:
raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: HTTP Error 429: Too Many Requests
但现在有或没有time.sleep()
它甚至不执行第一行,只是从一开始就因错误而中断。知道该怎么做吗?