标签:shelf 增删改 x11 参数 键值 level def sel 数据
1 import pickle 2 class Cat: 3 def __init__(self, name, age): 4 self.name = name 5 self.age = age 6 def catchMouse(self): 7 print(self.name, "抓??") 8 c = Cat("jerry", 18) 9 bs = pickle.dumps(c) # 序列化一个对象. 10 print(bs) # 一堆二进制. 看不懂 11 cc = pickle.loads(bs) # 把二进制反序列化成我们的对象 12 cc.catchMouse() # 猫依然是猫. 还可以抓老鼠
把对象写入文件
1 f = open("cat", mode="wb") 2 pickle.dump(c, f) # 写入到文件中 3 f.close() 4 5 f = open("cat", mode="rb") 6 cc = pickle.load(f) # 从文件中读取对象 7 cc.catchMouse()
多对象写入
1 lst = [Cat("jerry", 19), Cat("tommy", 20), Cat("alpha", 21)] 2 f = open("cat", mode="wb") 3 for el in lst: 4 pickle.dump(el, f) # 写入到文件中 5 f.close() 6 7 f = open("cat", mode="rb") 8 for i in range(len(lst)): 9 cc = pickle.load(f) # 从文件中读取对象 10 cc.catchMouse()
记住, 不能一行一行的读. 那真的要写入或者读取多个内容怎么办? 很简单. 装list里. 然后读取和写入都用list
1 lst = [Cat("jerry", 19), Cat("tommy", 20), Cat("alpha", 21)] 2 f = open("cat", mode="wb") 3 pickle.dump(lst, f) 4 f = open("cat", mode="rb") 5 ll = pickle.load(f) 6 for el in ll: 7 el.catchMouse()
1 import shelve 2 shelf = shelve.open("sylar") 3 # shelf["jay"] = "周杰伦" 4 print(shelf[‘jay‘]) 5 shelf.close() 6 7 s = shelve.open("sylar") 8 # s["jay"] = {"name":"周杰伦", "age":18, "hobby":"哄孩"} 9 print(s[‘jay‘]) 10 s.close() 11 12 修改内容,需要回写 13 s = shelve.open("sylar", writeback=True) # writeback=True可以动态的把我们修改的信息写入到文件中 14 s[‘jay‘][‘name‘] = "胡辣汤" # 尝试改变字典中的数据 15 s.close() 16 s = shelve.open("sylar") 17 print(s[‘jay‘]) # 改变了. 18 s.close() 19 20 删除 21 s = shelve.open("sylar", writeback=True) 22 del s[‘jay‘] 23 s.close() 24 25 s = shelve.open("sylar") 26 print(s[‘jay‘]) # 报错了, 没有了 27 s.close() 28 29 修改 30 s = shelve.open("sylar", writeback=True) 31 s[‘jay‘] = "周杰伦" 32 s[‘wlj‘] = "王宏" 33 s.close() 34 35 遍历 36 s = shelve.open("sylar") 37 for k in s: # 像字典一样遍历 38 print(k) 39 print(s.keys()) # 拿到所有key的集合 40 41 for k in s.keys(): 42 print(k) 43 44 for k, v in s.items(): # 像字典一样操作 45 print(k, v) 46 s.close()
1 import json 2 dic = {"a": "王", "b": "萝莉", "c": "清新"} 3 s = json.dumps(dic) # 把字典转化成json字符串 4 print(s) # {"a": "\u5973\u738b", "b": "\u841d\u8389", "c":"\u5c0f\u6e05\u65b0"} # 由于json中默认支持ASCII
解决\u问题(把字典——>json字符串)
1 import json 2 dic = {"a": "王", "b": "萝莉", "c": "清新"} 3 s = json.dumps(dic, ensure_ascii=False) # 把字典转化成json字符串 4 print(s) # {"a": "王", "b": "萝莉", "c": "清新"}
把json字符串——>字典
1 import json 2 s = ‘{"a": "王", "b": "萝莉", "c": "清新"}‘ 3 dic = json.loads(s) 4 print(type(dic), dic)
1 dic = {"a": "?王", "b": "萝莉", "c": "?清新"} 2 f = open("test.json", mode="w", encoding="utf-8") 3 json.dump(dic, f, ensure_ascii=False) # 把对象打散成json写?到?件中 4 f.close()
1 f = open("test.json", mode="r", encoding="utf-8") 2 dic = json.load(f) 3 f.close() 4 print(dic)
可向同一文件写入多个json串,但读不行。使用下面方法
1 import json 2 lst = [{"a": 1}, {"b": 2}, {"c": 3}] 3 # 写入 4 f = open("test.json", mode="w", encoding="utf-8") 5 for el in lst: 6 s = json.dumps(el, ensure_ascii=True) + "\n" 7 f.write(s) 8 f.close() 9 10 # 读取 11 f = open("test.json", mode="r", encoding="utf-8") 12 for line in f: 13 dic = json.loads(line.strip()) 14 print(dic) 15 f.close()
1 [DEFAULT] [DEFAULT] 2 ServerAliveInterval = 45 3 Compression = yes 4 CompressionLevel = 9 5 ForwardX11 = yes 6 [[bitbucket.org]] 7 User = hg 8 [[topsecret.server.com]] 9 Port = 50022 10 ForwardX11 = no
1 import configparser 2 config = configparser.ConfigParser() 3 config[‘DEFAULT‘] = { 4 "sleep": 1000, 5 "session-time-out": 30, 6 "user-alive": 999999 7 } 8 config[‘TEST-DB‘] = { 9 "db_ip": "192.168.17.189", 10 "port": "3306", 11 "u_name": "root", 12 "u_pwd": "123456" 13 } 14 config[‘168-DB‘] = { 15 "db_ip": "152.163.18.168", 16 "port": "3306", 17 "u_name": "root", 18 "u_pwd": "123456" 19 } 20 config[‘173-DB‘] = { 21 "db_ip": "152.163.18.173", 22 "port": "3306", 23 "u_name": "root", 24 "u_pwd": "123456" 25 } 26 f = open("db.ini", mode="w") 27 config.write(f) # 写??件 28 f.flush() 29 f.close()
1 config = configparser.ConfigParser() 2 config.read("db.ini") # 读取文件 3 print(config.sections()) # 获取到section. 章节...DEFAULT是给每个章节都配备的信息 4 print(config.get("DEFAULT", "SESSION-TIME-OUT")) # 从xxx章节中读取到xxx信息 5 # 也可以像字典一样操作 6 print(config["TEST-DB"][‘DB_IP‘]) 7 print(config["173-DB"]["db_ip"]) 8 for k in config[‘168-DB‘]: 9 print(k) 10 for k, v in config["168-DB"].items(): 11 print(k, v) 12 print(config.options(‘168-DB‘)) # 同for循环,找到‘168-DB‘下所有键 13 print(config.items(‘168-DB‘)) #找到‘168-DB‘下所有键值对 14 print(config.get(‘168-DB‘,‘db_ip‘)) # 152.163.18.168 get?法Section下的key对应的value
增删改操作
1 # 先读取. 然后修改. 最后写回文件 2 config = configparser.ConfigParser() 3 config.read("db.ini") # 读取文件 4 # 添加一个章节 5 # config.add_section("189-DB") 6 # config["189-DB"] = { 7 # "db_ip": "167.76.22.189", 8 # "port": "3306", 9 # "u_name": "root", 10 # "u_pwd": "123456" 11 # } 12 # 修改信息 13 config.set("168-DB", "db_ip", "10.10.10.168") 14 15 # 删除章节 16 config.remove_section("173-DB") 17 18 # 删除元素信息 19 config.remove_option("168-DB", "u_name") 20 21 # 写回文件 22 config.write(open("db.ini", mode="w"))
标签:shelf 增删改 x11 参数 键值 level def sel 数据
原文地址:https://www.cnblogs.com/hq82/p/9792002.html