Python 3.x 用于多地理坐标浮动错误的Openweathermap API

Python 3.x 用于多地理坐标浮动错误的Openweathermap API,python-3.x,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 ge

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

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。

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

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

你必须去掉你的数值周围的括号。因此,这将是:

url = "http://api.openweathermap.org/data/2.5/weather?lat=0&lon=1&units=imperial&appid=" 
这对我来说很好,希望对你也一样