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

时间:2019-05-14 16:45:30

标签: python

我有3个经纬度和一个正在构建的URL。我的输出应该是3个URL,对于每个经度/纬度,我将收到6个URL。我需要在下面的代码中进行哪些更改才能打印3个URL,而不是6个URL? try块和first for循环启动是错误处理,如果脚本失败,请尝试两次。即使脚本没有失败,我也得到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`

1 个答案:

答案 0 :(得分:0)

可能是try:和except:块。第一次失败。我猜你不需要在range(1)中尝试第二个循环。实际上,这里不需要任何循环。