我有以下脚本
#!/Users/admin/anaconda/bin/python
import geocoder
import unicodecsv
import logging
import csv
import time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
pcode=[]
with open('locs2.csv', 'rb') as f:
reader = csv.DictReader(f)
for line in reader:
lat = float(line['lat'])
lon = float(line['lon'])
g = geocoder.google([lat,lon], method='reverse')
attempts = 1 # number of lookups
while not(g.ok) and attempts < 4:
logging.warning('Geocoding ERROR: {}'.format(g.debug()))
time.sleep(2) # 2 seconds are specified in the API. If you stillget errors, it's because you've reached the daily quota.
g = geocoder.google([lat,lon], method='reverse')
attempts += 1
if attempts > 3:
logging.warning('Daily quota of google lookups exceeded.')
break
pcode.append((lat, lon, g.postal))
logging.info('Geocoding SUCCESS: ({},{},{})'.format(lat,lon,pcode))
我试图将脚本的所有结果放在一个文本文件中,但下面的命令行却没有用。为什么呢?
sys.stdout = open ("Output.txt", "w")
答案 0 :(得分:1)
更改日志配置以将日志发送到文件:
LOG_FILENAME = 'Output.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO)