针对多个地理坐标的Openweathermap API浮动错误

时间:2017-03-11 19:28:08

标签: python-3.x

我有一个文本文件,其中包含多个城市的地理坐标对

import requests
import smtplib
import urllib


def get_location():
    geocode={}
    latlon=open('latlononly.txt', 'r')
    for line in latlon:
        (lat, lon)=line.split(',')
        geocode[lat]=lon.strip()
    return geocode
def get_weather(geocode):
    api='xxxx'
    for x, y in geocode.items():
        url='http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&units=imperial&appid='+api+''.format(x,y)
        weather_r=requests.get(url)
        weather_j=weather_r.json()
        print(weather_j)

geocode=get_location()
get_location()
get_weather(geocode)
print(geocode)

错误是'{'cod':'400','message':'{0}不是浮动'}'

如何将.format()翻译成浮点数?或者我之前是否已将地理编码字典更改为浮点数?我想读取txt文件,以便我可以动态更改它,而不是使用Openweathermap API用于多个位置。

2 个答案:

答案 0 :(得分:1)

您必须删除您所评价的数字周围的括号。所以它会是:

url = "http://api.openweathermap.org/data/2.5/weather?lat=0&lon=1&units=imperial&appid=" 

这对我来说效果很好,所以对你也很有希望!

答案 1 :(得分:0)

问题如下:您尝试格式化空字符串。 解决方案如下:

url = 'http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&units=imperial&appid={2}'.format(x, y, api)