帮宣传下彩印网(www.caiyin.com) 有印刷,广告等等方面的需求就找这个网站吧,没错的。
天气预报在MongoDB中的天气预报的存储方式是:
/* 1 */ { "_id" : ObjectId("5a72ae6e6bd36a9f61e8ce3d"), "city" : "北京", "info" : [ { "index_of_living" : [ { "assess" : "最弱", "content" : "辐射弱,涂擦SPF8-12防晒护肤品。", "name" : "紫外线指数" }, { "assess" : "较易波动", "content" : "血糖较易波动,注意监测。", "name" : "健臻·血糖指数" }, { "assess" : "较易发", "content" : "温差较大,较易感冒,注意防护。", "name" : "感冒指数" }, { "assess" : "冷", "content" : "建议着棉衣加羊毛衫等冬季服装。", "name" : "穿衣指数" }, { "assess" : "较适宜", "content" : "无雨且风力较小,易保持清洁度。", "name" : "洗车指数" }, { "assess" : "中", "content" : "易感人群应适当减少室外活动。", "name" : "空气污染扩散指数" } ], "K" : "5℃", "weather" : "晴转多云转晴", "time" : "1日(今天)", "wind" : "<3级转3-4级", "V" : "-7℃" }, { "index_of_living" : [ { "assess" : "弱", "content" : "辐射较弱,涂擦SPF12-15、PA+护肤品。", "name" : "紫外线指数" }, { "assess" : "易波动", "content" : "气温多变,血糖易波动,请注意监测。", "name" : "健臻·血糖指数" }, { "assess" : "极易发", "content" : "强降温,天气寒冷,极易发生感冒。", "name" : "感冒指数" }, { "assess" : "寒冷", "content" : "建议着厚羽绒服等隆冬服装。", "name" : "穿衣指数" }, { "assess" : "较适宜", "content" : "无雨且风力较小,易保持清洁度。", "name" : "洗车指数" }, { "assess" : "良", "content" : "气象条件有利于空气污染物扩散。", "name" : "空气污染扩散指数" } ], "K" : "-1℃", "weather" : "晴", "time" : "2日(明天)", "wind" : "3-4级转<3级", "V" : "-9℃" }, { "index_of_living" : [ { "assess" : "最弱", "content" : "辐射弱,涂擦SPF8-12防晒护肤品。", "name" : "紫外线指数" }, { "assess" : "易波动", "content" : "血糖易波动,注意监测。", "name" : "健臻·血糖指数" }, { "assess" : "较易发", "content" : "天较凉,增加衣服,注意防护。", "name" : "感冒指数" }, { "assess" : "冷", "content" : "建议着棉衣加羊毛衫等冬季服装。", "name" : "穿衣指数" }, { "assess" : "较适宜", "content" : "无雨且风力较小,易保持清洁度。", "name" : "洗车指数" }, { "assess" : "中", "content" : "易感人群应适当减少室外活动。", "name" : "空气污染扩散指数" } ], "K" : "0℃", "weather" : "多云", "time" : "3日(后天)", "wind" : "<3级", "V" : "-9℃" }, { "index_of_living" : [ { "assess" : "弱", "content" : "辐射较弱,涂擦SPF12-15、PA+护肤品。", "name" : "紫外线指数" }, { "assess" : "易波动", "content" : "血糖易波动,注意监测。", "name" : "健臻·血糖指数" }, { "assess" : "较易发", "content" : "天较凉,增加衣服,注意防护。", "name" : "感冒指数" }, { "assess" : "冷", "content" : "建议着棉衣加羊毛衫等冬季服装。", "name" : "穿衣指数" }, { "assess" : "较适宜", "content" : "无雨且风力较小,易保持清洁度。", "name" : "洗车指数" }, { "assess" : "中", "content" : "易感人群应适当减少室外活动。", "name" : "空气污染扩散指数" } ], "K" : "1℃", "weather" : "晴", "time" : "4日(周日)", "wind" : "<3级", "V" : "-8℃" }, { "index_of_living" : [ { "assess" : "弱", "content" : "辐射较弱,涂擦SPF12-15、PA+护肤品。", "name" : "紫外线指数" }, { "assess" : "易波动", "content" : "血糖易波动,注意监测。", "name" : "健臻·血糖指数" }, { "assess" : "较易发", "content" : "天较凉,增加衣服,注意防护。", "name" : "感冒指数" }, { "assess" : "冷", "content" : "建议着棉衣加羊毛衫等冬季服装。", "name" : "穿衣指数" }, { "assess" : "较适宜", "content" : "无雨且风力较小,易保持清洁度。", "name" : "洗车指数" }, { "assess" : "中", "content" : "易感人群应适当减少室外活动。", "name" : "空气污染扩散指数" } ], "K" : "0℃", "weather" : "晴", "time" : "5日(周一)", "wind" : "<3级", "V" : "-10℃" }, { "index_of_living" : [ { "assess" : "弱", "content" : "辐射较弱,涂擦SPF12-15、PA+护肤品。", "name" : "紫外线指数" }, { "assess" : "较易波动", "content" : "血糖较易波动,注意监测。", "name" : "健臻·血糖指数" }, { "assess" : "较易发", "content" : "天较凉,增加衣服,注意防护。", "name" : "感冒指数" }, { "assess" : "冷", "content" : "建议着棉衣加羊毛衫等冬季服装。", "name" : "穿衣指数" }, { "assess" : "较适宜", "content" : "无雨且风力较小,易保持清洁度。", "name" : "洗车指数" }, { "assess" : "中", "content" : "易感人群应适当减少室外活动。", "name" : "空气污染扩散指数" } ], "K" : "2℃", "weather" : "晴", "time" : "6日(周二)", "wind" : "<3级", "V" : "-8℃" }, { "index_of_living" : [ { "assess" : "弱", "content" : "辐射较弱,涂擦SPF12-15、PA+护肤品。", "name" : "紫外线指数" }, { "assess" : "较易波动", "content" : "血糖较易波动,注意监测。", "name" : "健臻·血糖指数" }, { "assess" : "较易发", "content" : "天较凉,增加衣服,注意防护。", "name" : "感冒指数" }, { "assess" : "冷", "content" : "建议着棉衣加羊毛衫等冬季服装。", "name" : "穿衣指数" }, { "assess" : "较适宜", "content" : "无雨且风力较小,易保持清洁度。", "name" : "洗车指数" }, { "assess" : "中", "content" : "易感人群应适当减少室外活动。", "name" : "空气污染扩散指数" } ], "K" : "3℃", "weather" : "晴转多云", "time" : "7日(周三)", "wind" : "<3级", "V" : "-8℃" } ] }
就是每个地方的未来的七天的天气
import json import pymongo #建立连接 client = pymongo.MongoClient("mongodb://root:root@10.1.252.123:27017") #指定数据库 db = client[‘weather‘] #指定集合 collection= db[‘weather_tian‘] #指定要查询的内容 result= collection.find() print(‘type result===‘,type(result)) fp = open(‘./output_weather.json‘,‘a+‘) cal_index =0 for i in result: cal_index = cal_index +1 if cal_index == 4: #先打个三个地区出来看看情况 break #print(‘type i =‘,type(i)) i.pop(‘_id‘) #不如这个key和value不去掉的话,会报ObjectId错误的问题,因为这个是MongoDB里面的类,json不认识,你的自定定义这个类来处理 j = json.dumps(i,ensure_ascii=False) fp.write(j) fp.write(‘\n‘) #这样每个地区的未来七天的天气情况就写为一行了 fp.close()
这样就在本地存了个json文件了。有三个地区的天气。
接下来我们是从这个json文件读入,变成dict,代码如下
import json import time def json_file_to_dict(): ofile = open(‘./output_weather.json‘) for line in ofile.readlines(): #print(line) print(type(line)) afterjson = json.loads(line) print(afterjson[‘city‘]) #其中city是dict中的某个key if __name__ == ‘__main__‘: json_file_to_dict()