Crossin的编程教室

标题: 求高手解惑:为什么查天气的这个的循环至16时会报错,.. [打印本页]

作者: x8344364    时间: 2017-10-28 23:12
标题: 求高手解惑:为什么查天气的这个的循环至16时会报错,..
#coding:utf-8
import urllib2
url1 = 'http://api.k780.com:88/?app=weather.city&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json'
content = urllib2.urlopen(url1).read()
# print content
import json
data = json.loads(content)
# print data
result2={}
# print result2

        # print data["success"]
result =data["result"]
        # print result

for i in range(16,2651):
        print i
        for rel in result[str(i)]:                       
                result2[result[str(i)]['citynm']]=result[str(i)]['cityid']
                                # print result2       
                print result2       

# print result2


1Traceback (most recent call last):6

  File "F:\PythonTest\CheckWeather.py", line 23, in <module>
    for rel in result[str(i)]:                       
KeyError: '16'
[Finished in 0.9s]
作者: x8344364    时间: 2017-10-28 23:31
我知道了,键值在字典中不存的,要加个判断语句!!




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5