序列化:
序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes.
为什么要序列化:
有种办法可以直接把内存数据(eg:10个列表,3个嵌套字典)存到硬盘上,下次程序再启动,再从硬盘上读回来,还是原来的格式。
1.把内存数据(eg:dict)转成字符串叫 序列化
2.把字符串转成内存数据类型叫 反序列化
python模块能自动转换 json pickle 作用:内存得数据很容易存下来,也很容易读回来,因为写到硬盘上只认识str bytes
-------------------------------------------------------------
json 模块
json.dump(d,f) json.load(f) #与文件得交互 dump(可多次,但不那样做) load(只可一次) 把数据类型转成字符串存到内存里得意义? json.dumps(data) json.loads(q) #与内存得交互 1.把内存数据 通过网络 共享给远程其他人 必须:bytes 2.定义了不同语言之间得交互规则 2.1 纯文本:坏处不能共享复杂得数据类型 18:32 424224 iphone 5000 2.2 xml 占得空间大 效率低 <data> <country> <year>2018</year> # year:2018 </country> </data> 2.3 json 简单 可读性好 data = { 'roles':[ {'role':'monster','type':'pig','life':50}, {'role':'hero','type':'关羽','life':80} ] } import json data = { 'roles':[ {'role':'monster','type':'pig','life':50}, {'role':'hero','type':'关羽','life':80} ] } s = json.dumps(data) print(s,type(s)) data = json.loads(s) print(data,type(data),data['roles']) json.dump(data,open('test.json','w',encoding='utf-8')) data = json.load(open('test.json','r',encoding='utf-8')) print(data['roles'],type(data))
pickle 模块
rb wb 和json得四个方法一样 写读 都是bytes形式的 可以将函数dump load都行 pickle.dumps(d) json.loads(d) pickle.dump(d,pk) pickle.load(pk) import pickle data = { 'roles':[ {'role':'monster','type':'pig','life':50}, {'role':'hero','type':'关羽','life':80} ] } def sayhi(): print('sayhi') s = pickle.dumps(data) print(s,type(s)) data = pickle.loads(s) print(data,type(data),data['roles']) pickle.dump(data,open('test.json','wb')) data = pickle.load(open('test.json','rb')) print(data,type(data)) s = pickle.dumps(sayhi) print(s) data = pickle.loads(s) data() pickle.dump(sayhi,open('test1.json','wb')) pickle.load(open('test1.json','rb'))()
json 和 pickle 得区别是:
json 转化得数据类型:str int list tuple dict 不支持set
pickle 支持python里得所有数据类型 确定是 只能在python里使用 函数都可以序列化
shelve 模块
pickle封装了shelve 只能在python中用
序列化: import shelve f = shelve.open('shelve_test') # 打开一个文件 names = ["alex", "rain", "test"] info = {'name':'alex','age':22} f["names"] = names # 持久化列表 f['info_dic'] = info f.close() 反序列化: import shelve d = shelve.open('shelve_test') # 打开一个文件 print(d['names']) print(d['info_dic']) #del d['test'] #还可增加 删除 可整体重新赋值
xml 模块
作用:
不同语言之间内存数据得交换
内存得数据可转换成xml存到硬盘上
1.xml的格式如下,就是通过<>节点来区别数据结构的: <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data> 2.xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml") root = tree.getroot() print(root.tag) #遍历xml文档 for child in root: print(child.tag, child.attrib) for i in child: print(i.tag,i.text) #只遍历year 节点 for node in root.iter('year'): print(node.tag,node.text) 3.修改和删除xml文档内容 import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml") root = tree.getroot() #修改 for node in root.iter('year'): new_year = int(node.text) + 1 node.text = str(new_year) node.set("updated","yes") tree.write("xmltest.xml") #删除node for country in root.findall('country'): rank = int(country.find('rank').text) if rank > 50: root.remove(country) tree.write('output.xml') 4.自己创建xml文档 import xml.etree.ElementTree as ET new_xml = ET.Element("namelist") name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"}) age = ET.SubElement(name,"age",attrib={"checked":"no"}) sex = ET.SubElement(name,"sex") sex.text = '33' name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"}) age = ET.SubElement(name2,"age") age.text = '19' et = ET.ElementTree(new_xml) #生成文档对象 et.write("test.xml", encoding="utf-8",xml_declaration=True) ET.dump(new_xml) #打印生成的格式
configparser 模块
此模块用于生成和修改常见配置文档
1.来看一个好多软件的常见配置文件格式如下***.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no 2.解析配置文件 >>> import configparser >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: print(key) ... user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes' 3.其它增删改查语法 [group1] k1 = v1 k2:v2 [group2] k1 = v1 import ConfigParser config = ConfigParser.ConfigParser() config.read('i.cfg') # ########## 读 ########## #secs = config.sections() #print secs #options = config.options('group2') #print options #item_list = config.items('group2') #print item_list #val = config.get('group1','k1') #val = config.getint('group1','key') # ########## 改写 ########## #sec = config.remove_section('group1') #config.write(open('i.cfg', "w")) #sec = config.has_section('wupeiqi') #sec = config.add_section('wupeiqi') #config.write(open('i.cfg', "w")) #config.set('group2','k1','11111') #config.write(open('i.cfg', "w")) #config.remove_option('group2','age') #config.write(open('i.cfg', "w"))
总结:
1.json
2.pickle
3.shelve
4.xml
5.configparser