python 入门9

Python pickle模块

    我们在之前的学习中已经学习过了list,dict,元组等用来保存数据的对象类型,在日常的代码编辑中我们往往需要尽可能对代码进行封装,提高代码的封闭性和可读性,因此我们往往把程序中一些用于查询的数据保存为单独的一个文件。程序只执行对该文件的读取和修改。

    我们之前也学过了对文件的创建写入和读取,美中不足的是我们之前都是字符串的形式对文件进行的写入和读取,这对于我们程序读取其中的一类对象产生了不小的挑战,我们需要对文件中的各个元素进行判断然后在转换成某一类特定的对象类型。

    幸运的是在python中引入了pickle(泡菜)这一模块,他可以采取传统的文件读取写入方式,不同的是他保存的是以二进制的形式,我们在读取时读到的二进制转换成的某一特定类型对象,这使得我们再也不需要人为去想办法保存和读取非字符串的数据类型了。

    实例:我们做一个查询天气的列子,首先我们将各个城市的名称及其在天气网站对应的代码组成一个字典保存到一个pickle文件:

city=dict(北京='101010100',        
上海='101020100',          
天津='101030100',          
重庆='101040100',          
济南='101120101',          
石家庄='101090101',     
长春='101060101',          
哈尔滨='101050101',     
沈阳='101070101',          
呼和浩特='101080101',        
乌鲁木齐='101130101',    
兰州='101160101',      
银川='101170101',      
太原='101100101',      
西安='101110101',      
郑州='101180101',      
合肥='101220101',      
南京='101190101',      
杭州='101210101',     
福州='101230101',    
广州='101280101',     
南昌='101240101',   
海口='101310101',    
南宁='101300101',    
贵阳='101260101',     
长沙='101250101',    
武汉='101200101',     
成都='101270101',      
昆明='101290101',      
拉萨='101140101',     
西宁='101150101',      
台北县='101340101',
香港='101320101' ,  
澳门='101330101'  )
import pickle
f=open('d:\\city.pkl','wb')
pickle.dump(city,f)
f.close()

以上程序会在D盘下生成一个city.pkl文件并把所有城市及其对应城市编码,保存到该文件下。接下来我们可以查询对应城市编码然后得到该城市天气情况。

import urllib.request
import gzip
import json
import pickle
print('------天气查询------')
def get_weather_data() :
    city_name = input('请输入要查询的城市名称:')
    url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
    pickle_file=open("city.pkl","rb")
    city=pickle.load(pickle_file)
    url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey='+city[city_name]
    #网址1只需要输入城市名,网址2需要输入城市代码
    #print(url1)
    weather_data = urllib.request.urlopen(url2).read()
    #读取网页数据
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    #解压网页数据
    weather_dict = json.loads(weather_data)
    #将json数据转换为dict数据
    return weather_dict

def show_weather(weather_data):
    weather_dict = weather_data 
    #将json数据转换为dict数据
    if weather_dict.get('desc') == 'invilad-citykey':
        print('你输入的城市名有误,或者天气中心未收录你所在城市')
    elif weather_dict.get('desc') =='OK':
        forecast = weather_dict.get('data').get('forecast')
        print('城市:',weather_dict.get('data').get('city'))
        print('温度:',weather_dict.get('data').get('wendu')+'℃ ')
        print('感冒:',weather_dict.get('data').get('ganmao'))
        print('风向:',forecast[0].get('fengxiang'))
        print('风级:',forecast[0].get('fengli'))
        print('高温:',forecast[0].get('high'))
        print('低温:',forecast[0].get('low'))
        print('天气:',forecast[0].get('type'))
        print('日期:',forecast[0].get('date'))
        print('*******************************')
        four_day_forecast =input('是否要显示未来四天天气,是/否:')
        if four_day_forecast == '是' or 'Y' or 'y':
            for i in range(1,5):
                print('日期:',forecast[i].get('date'))
                print('风向:',forecast[i].get('fengxiang'))
                print('风级:',forecast[i].get('fengli'))
                print('高温:',forecast[i].get('high'))
                print('低温:',forecast[i].get('low'))
                print('天气:',forecast[i].get('type'))
                print('--------------------------')
    print('***********************************')

show_weather(get_weather_data())




发布了15 篇原创文章 · 获赞 5 · 访问量 2164

猜你喜欢

转载自blog.csdn.net/bojin4564/article/details/80262304