模块之序列化,pickle模块,shelve模块,json模块,configparser模块
#pickle 模块 import pickle # dumps 序列化。 把对象转化成bytes # loads 反序列化。 把bytes转化成对象 # dump 序列化。 把对象转化成bytes并写入文件 # load 反序列化。把文件中的bytes读取。转化成对象 class Cat: def __init__(self, name, age): self.name = name self.age = age def catchMouse(self): print(self.name, self.age, "抓老鼠") c = Cat("jerry", 18) c.catchMouse() # dumps 把对象转化成bytes 序列化 bs = pickle.dumps(c) print(bs) # 把bytes 转换回对象 反序列化 ccc = pickle.loads(bs) ccc.catchMouse() dic = {"jay": "周杰伦", "jj": "大阳哥"} bs = pickle.dumps(dic) print(bs) d = pickle.loads(bs) print(d) c = Cat("jerry", 18) f = open("pickle-test", mode="wb") pickle.dump(c, f) # 结果人是看不了的。 f.close() f = open("pickle-test", mode="rb") c = pickle.load(f) c.catchMouse() lst = [Cat("猫1", 10), Cat("猫2", 9), Cat("猫3", 9), Cat("猫4", 9), Cat("猫5", 9)] f = open("pickle-test", mode="wb") pickle.dump(lst, f) # for el in lst: # pickle.dump(el, f) f.flush() f.close() f = open("pickle-test", mode="rb") while 1: try: c1 = pickle.load(f) c1.catchMouse() except EOFError: break
#shelve模块 import shelve # 打开一个文件 f = shelve.open("大阳哥", writeback=True) f['jj'] = "林俊杰" f['dyg'] = "大阳哥" f['zzr'] = "周芷若" f = {} # 像操作字典一样操作文件 f["jay"] = "周杰伦" print(f['jay']) f["jay"] = {'name': "周杰伦", 'age': 38, "hobby": "吃喝拉撒睡"} f['jay']['name'] = "胡辣汤" print(f['jay']['name']) print(f['jay']) f.close() f = shelve.open("大阳哥") print(f.keys()) for k in f.keys(): # 可以获取到所有的key print(k) for k in f: print(k) for k, v in f.items(): print(k, v)
#json模块 wf = { "name": "汪峰", "age": 18, "hobby": "抢头条", "wife": { "name": "子怡", "age": 19, "hobby":["唱歌", "跳舞", "演戏"] } } #wf['wife']['hobby'] class Person: def __init__(self, name, age, hobby, wife): self.name = name import json dic = {"a": '理查德姑妈', "b": "找到你", "c": "看不见的客人"} # s = json.dumps(dic) # 如果你的key或者value超出了ascii范畴。 就会显示成\uxxxxx s = json.dumps(dic, ensure_ascii=False) # 干掉ascii码 print(repr(s), type(s)) # # 把字符串解析成 字典 dic1 = json.loads(s) print(dic1, type(dic1)) # 写入 f = open("waimai.json", mode="w", encoding="utf-8") json.dump(dic, f, ensure_ascii=False) # 把json写入到文件中 f.close() # # # 读出 f = open("waimai.json", mode="r", encoding="utf-8") s = json.load(f) # 把文件中的json串。读取成字典 print(s, type(s)) lst = [{"a": "胡辣汤"},{"b":"吱吱冒油的大猪蹄子"},{"c": "盖浇饭"},{"d":"马拉"},{"e":"法国大蜗牛"}] f = open("menu.json", mode="w", encoding="utf-8") for dic in lst: json.dump(dic, f, ensure_ascii=False) f.close() f = open("menu.json", mode="r", encoding="utf-8") s = json.load(f) print(s) # 写入的时候 # 1. 循环 # 2. 用dumps把字典转化成字符串, 然后手工在后面加一个\n # 3. 写出 f = open("new_menu.json", mode="w", encoding="utf-8") lst = [{"a": "胡辣汤"},{"b":"吱吱冒油的大猪蹄子"},{"c": "盖浇饭"},{"d":"马拉"},{"e":"法国大蜗牛"}] for el in lst: s = json.dumps(el, ensure_ascii=False) + "\n" f.write(s) f.flush() f.close() # 读取的时候 # 1. for line in f: # 2. strip()去掉空白 # 3. loads()变成字典 # f = open("new_menu.json", mode="r", encoding="utf-8") # for line in f: # line = line.strip() # dic = json.loads(line) # print(dic)
#configparser模块 import configparser conf = configparser.ConfigParser() conf["DEFAULT"] = { "session-time-out":30, "user-alive": 60, "connect-alive":10 } conf["189-DB"] = { "ip": "189.135.63.12", "port": 3306, "uname": "root", "password": "root" } conf["166-DB"] = { "ip": "189.135.63.12", "port": 3306, "uname": "root", "password": "root" } conf["163-DB"] = { "ip": "189.135.63.12", "port": 3306, "uname": "root", "password": "root" } f = open("db.ini", mode="w") conf.write(f) # 把文件扔进去。 写到这个文件里 # 读取内容 conf = configparser.ConfigParser() conf.read("db.ini") print(conf.sections()) # 获取到章节 keys() print(conf['166-DB']["ip"]) # 可以像字典一样操作 print(conf["166-DB"]["port"]) print(conf["166-DB"]["uname"]) print(conf["166-DB"]["password"]) for key in conf['163-DB']: print(key) for key, value in conf['163-DB'].items(): print(key, value) # 增删改操作 conf = configparser.ConfigParser() conf.read("db.ini") # 读取出来 conf['163-DB']['uname'] = "alex" # del conf['163-DB']["password"] conf.set("163-DB", "wangermazi", "189") # setattr conf.add_section("jay") conf.write(open("db.ini", mode="w"))