为什么python循环会产生双重结果?

2024-10-05 14:28:23 发布

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

我有3个lat/lngs和一个我正在构建的URL。我的输出应该是3个URL,对于每个lat/lng,我收到6个。我需要在下面的代码中更改什么来打印3个URL而不是6个?try块和first for循环start是错误处理,如果脚本失败,请重试两次。我得到6个值,即使脚本没有失败

 def main():
    for i in range(2):
        for attempts in range (1):
            try:
                for lat, lon, id_, startDate, endDate in zip(latval, lonval, idVal, startDayValStr, endDayValStr):
                    time_param = '?start='+ startDate +'T'+ "01:00" + 'Z' + '&end='+ endDate + 'T' + "23:00"  + 'Z'
                    hrPrecip = 'https://insight.api.wdtinc.com/hourly-precipitation/' + str(lat)+'/' + str(lon) + time_param + '&unit=inches'
                    print hrPrecip
            except Exception as e:
                attempts = i + 1
                sleep(30)
                print "now trying attempt #" +  " " + str(attempts) + " " + "for error"  " " + str(e)
                print(traceback.format_exc())
                logging.exception(e)
                msg = "PYTHON ERRORS:\nTraceback info:\n" + traceback.format_exc()
                logging.debug("sending error email")
                emailserver.email_error_msg(msg)
if __name__ == "__main__":
    main()

输出:

https://insight.api.wdtinc.com/hourly-precipitation/44.797207/-95.175648?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.796302/-95.180946?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.778728/-95.23022?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.797207/-95.175648?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.796302/-95.180946?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.778728/-95.23022?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches`

Tags: httpscomapiurlforunitstartend