如何将此脚本的结果放入fi

2024-10-02 16:29:11 发布

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

我有下面的剧本

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

Tags: csvinimporttimelogginggooglelineopen