zoukankan      html  css  js  c++  java
  • 序列化,json pickle,shelve

     

    1,序列化模块: json , pickle , shelve(了解)

    序列化模块:

    模块:一个py文件就是一个模块.

    模块分类:

    1.内置模块:登录模块,时间模块,sys模块,os模块等等.

    2.扩展模块(需要下载):itchat(与微信有关),爬虫:beautifulsoup.所有的扩展模块:http//pypi.org/

    3.自定义模块:自己写的模块.

    python开发效率之所以那么高是因为:python的模块非常多,还有第三方库.

    序列化模块:

    序列化:创造一个序列.(特殊处理序列化的字符串)

    实例化:创造一个实例(对象)

    文件中可以存储:字符串和bytes.

    数据的传输:bytes类型

    序列化:

    json:适用于不同语言之间的.但是可支持的数据类型:字符串,数字,列表,字典,bool.

    pickle: 只用于python语言之间的.可支持python所有的数据类型.

    shelve(了解):只是python,小工具(文件方面)

    json:数据通过网络发送给别人. 写入文件也用到json

    序列化过程:一个数据类型转化成序列化的字符串;

    反序列化过程:序列化的字符串转化成它所对应的数据类型.

    import json

    两队:

    dumps   loads

    dump    load

    dumps    loads    网络的传输

    dic = {"alex":["women","women","老女人"],"p":True}

    print(str(dic))  #基础数据类型str,里面如果有引号就是单引号

    ret = json.dumps(dic,ensure_ascii = False)  #序列化过程:数据类型dic转化成序列化的字符串,这个字符串是双引号的,跟上面的字符串不一样.

    print(ret,type(ret))

    被json序列化的字符串:

    1.可以直接通过网络互相传输,

    2.可以在各个语言中通用.

    dic1 = json.loads(ret)  #反序列化过程:将序列化的字符串转化成原有的数据类型.

    print(dic1,type(dic1))

    import  json

    li = ["张三","李四","王二麻子"]

    dump  load  有关文件存储

    f = open("json_file",encoding="utf-8",mode = "w")

    json.dump(li,f,ensure_ascii=False)  #将序列化的字符串存储到文件中

    f.close()

    f = open("json_file",encoding="utf-8")

    ret = json.load(f)

    print(ret,type(ret))

    f.close()

    有关文件存储的问题?

    dic = {"alex":["women","women","老女人"],"p":True}

    dic1 = {"alex1":["women","women","老女人"],"p":True}

    dic2 = {"alex2":["women","women","老女人"],"p":True}

    将多个序列化的字符串写入文件,然后反序列化,就会出错.

    用dump  load只能写入和读取文件一个序列化的字符串

    import  json

    f = open("json_file",encoding = "utf-8",mode = "w")

    json.dump(dic,f)

    json.dump(dic1,f)

    json.dump(dic2,f)

    f.close()

    f = open("json_file",encoding="utf-8")

    print(json.load(f))

    print(json.load(f))

    print(json.load(f))

    f.close()

    #上面这种操作会报错,解决办法看下面的操作.

    with open("json_file",encoding = "utf-8",mode="a")as f:

      f.write(json.dumps(dic)+" ")

      f.write(json.dumps(dic1)+" ")

      f.write(json.dumps(dic2)+" ")

    with open("json_file",encoding="utf-8")as f1:

      for i in f1:

        print(json.loads(i))

    其他参数:

    import  json

    dic = {"alex":["women","women","老女人"],"p":True}

    ret = json.dumps(dic,sort_keys = True,indent = 2,separators = (",",":"),ensure_ascii=False)

    print(ret)

    print(json.loaps(ret)) #如果改了:separators=("|","&")反序列化不能正常执行.就会报错.

    sort_keys = True字典键的首字母的ascii码排序.

    ensure_ascii = False 显示中文

    indent= 2  key缩进

    import  json

    dic ={(1,2,3):"alex",1:[1,2,3]}

    ret = json.dumps(dic)

    print(ret)   #会报错,TypeError: keys must be a string(键(这里的键)必须是字符串)

    2.pickle序列化模块,python语言网络交互使用的,它支持所有的数据类型.

    两对:四个方法  dumps   loads 用于网络传输.  dump   load用于文件操作

    dumps  loads  网络传输

    dic= {1:True ,(2,3):[1,2,3,4],False:{1,2,3,4}}

    import  pickle

    ret = pickle.dumps(dic)  #bytes类型(序列化的)无法识别内容

    dic1 = pickle.loads(ret)

    print(dic1,type(dic1))

    dump  load  文件操作

    dic = {1:True,(2,3):[1,2,3,4],False:{1,2,3}}

    import  pickle

    with open("pickle_file",mode = "wb")as f1:

      pickle.dump(dic,f1)

    with open("pick_file",mode = "rb")as f2:

      print(pickle.load(f2))

    多个数据存储到一个文件

    dic = {"alex": ('women','women','老女人')}
    dic2 = {"alex1": ('women','women','老女人')}
    dic3 = {"alex2": ('women','women','老女人')}

    import pickle

    with open("pickle_file",mode = "wb")as f1:

      pickle.dump(dic,f1)

      pickle.dump(dic2,f1)

      pickle.dump(dic3,f1)

    with open("pickle_files",mode = "rb")as f1:

      while True:

        try:

          print(pickle.load(f1))

        except EOFError:

          break

    shelve     与文件相关

    import  shelve

    f = shelve.open("shelve_file")

    f["key"] = {"int":10,"float":9.5,"string":"data"}  #直接对文件句柄操作,就可以存入数据.

    f.close()

    import  shelve

    f1 = shelve.open("shelve_file")

    existing = f1["key"]     #取出数据的时候也只需要直接用key获取即可,但是如果key不存在会报错,

    f1.close()

    print(existing)

    import  shelve

    f = shelve.open("shelve_file",flag = "r")

    f["key"] = [11,22,33]

    f.close()

    f = shelve.open("shelve_file",flag = "r")

    ex = f["key"]

    print(ex)

    f.close()

    f['key'] = {'int':10, 'float':9.5, 'string':'Sample data'}

    import shelve

    f1  = shelve.open("shelve_file")

    print(f1["key"])

    f1["key]["new_value"] = "this was not here before"

    print(f1["key"])  #没有添加进去

    f1.close()

    f2 = shelve.open("shelve_file",writeback = True)

    print(f2["key"]

    f2["key]["new_value"] = "this was not here before"

    print(f2["key"])  #添加进去了

    f2.close()

    主要是要在这个字典里面添加键值对,

  • 相关阅读:
    POJ 1251 Jungle Roads
    1111 Online Map (30 分)
    1122 Hamiltonian Cycle (25 分)
    POJ 2560 Freckles
    1087 All Roads Lead to Rome (30 分)
    1072 Gas Station (30 分)
    1018 Public Bike Management (30 分)
    1030 Travel Plan (30 分)
    22. bootstrap组件#巨幕和旋转图标
    3. Spring配置文件
  • 原文地址:https://www.cnblogs.com/fengkun125/p/9271563.html
Copyright © 2011-2022 走看看