好的,所以我在 python 中有一个命令来写入文件
from urllib.request import urlretrieve
urlretrieve('http://www.myweather2.com/developer/forecast.ashx?uac=29bsTnYaZT&output=json&query=50323&temp_unit=f','weatherfile.txt')
然后是打开该文件的命令
with open ('/home/pi/weatherfile.txt','r') as f:
print(f.read())
这是我得到的输出
{ "weather": { "curren_weather": [ {"humidity": "62", "pressure": "1007", "temp": "86", "temp_unit": "f", "weather_code": "1", "weather_text": "Partly cloudy", "wind": [ {"dir": "S", "speed": "6", "wind_unit": "kph" } ] } ], "forecast": [ {"date": "2013-06-26", "day": [ {"weather_code": "0", "weather_text": "Sunny skies", "wind": [ {"dir": "W", "dir_degree": "266", "speed": "11", "wind_unit": "kph" } ] } ], "day_max_temp": "93", "night": [ {"weather_code": "0", "weather_text": "Clear skies", "wind": [ {"dir": "NW", "dir_degree": "313", "speed": "22", "wind_unit": "kph" } ] } ], "night_min_temp": "68", "temp_unit": "f" }, {"date": "2013-06-27", "day": [ {"weather_code": "1", "weather_text": "Partly cloudy skies", "wind": [ {"dir": "NNE", "dir_degree": "27", "speed": "18", "wind_unit": "kph" } ] } ], "day_max_temp": "86", "night": [ {"weather_code": "29", "weather_text": "Thundery outbreaks possible", "wind": [ {"dir": "NNE", "dir_degree": "20", "speed": "18", "wind_unit": "kph" } ] } ], "night_min_temp": "65", "temp_unit": "f" } ] }}
我希望它输出更好一点有没有办法我可以做到这一点?
这是我尝试过的更新
froom urllib.request import urlretrieve
urlretrieve('http://api.wunderground.com/api/015098ae0bc15cce/conditions/q/IA/Urbandale.json','weatherfile.txt')
pp = pprint.PrettyPrinter(indent=4)
with open ('/home/pi/weatherfile.txt','r') as f:
d = json.loads(f.read())
pp.pprint(d)
我得到这个错误
Traceback (most recent call last):
File "C:/Users/Grant/Desktop/getweather.py", line 3, in <module>
pp = pprint.PrettyPrinter(indent=4)
NameError: name 'pprint' is not defined