2017-03-11 12 views
0

フロート私は、複数の都市の地理座標のペアを持つテキストファイルOpenweathermapのAPIがエラー

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) 

は、エラーが右の「{」タラということである持っています'{0}は浮動小数点ではありません'} '

.format()を浮動小数点に変換するにはどうすればよいですか?または、私はその前にフロートにジオコード辞書を変更しましたか?私はtxtファイルを読み込みたいので、Openweathermap APIを複数の場所に使用するのではなく、その場で変更することができます。

答えて

0

問題は次のとおりです。空の文字列をフォーマットしようとしました。 ソリューションは以下の通りです:

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

あなたは価値ある数の周りに括弧を削除する必要があります。だから、次のようになります。

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

これがうまくいけば、あなたのためだけでなく、私のためにうまく働きました!

関連する問題