json模块&pickle模块
序列化:序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes
序列化有两个模块:
- json:用于字符串 和 python数据类型间进行转换
- pickle:用于python特有的类型 和 python的数据类型间进行转换
Json模块提供了四个功能:dumps、dump、loads、load
pickle模块提供了四个功能:dumps、dump、loads、load
json与pickle的使用方法
import pickle
data = {'k1':123,'k2':'Hello'}
# pickle.dumps 将数据通过特殊的形式转换位只有python语言认识的字符串
p_str = pickle.dumps(data)
print(p_str)
#pickle.dump 将数据通过特殊的形式转换位只有python语言认识的字符串,并写入文件
with open('D:/result.pk','wb',encoding='utf8') as fp:
pickle.dump(data,fp)
import json
# json.dumps 将数据通过特殊的形式转换位所有程序语言都认识的字符串
j_str = json.dumps(data)
print(j_str)
#pickle.dump 将数据通过特殊的形式转换位只有python语言认识的字符串,并写入文件
with open('D:/result.json','wb',encoding='utf8') as fp:
json.dump(data,fp)
json与pickle区别:
JSON:
优点:跨语言、体积小
缺点:只能支持intstrlist upledict
Pickle:
优点:专为python设计,支持python所有的数据类型
缺点:只能在python中使用,存储数据占空间大
shelve模块
shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
shelve模块使用方法
import shelve
#序列化
# f = shelve.open('a')
# names = ["alex", "rain", "test"]
# info = {'name':'alex','age':22}
#
#
# f["names"] = names # 持久化列表
# f['info_dic'] = info
#
# f.close()
#反序列化
d = shelve.open('a') # 打开一个文件
# print(d['names'])
# print(d['info_dic'])
# d['names'] = [1,2,3,4] #修改,只能通过赋值方法来进行修改
# print(d['names'][1])
# del d['names'] # 删除
xml模块
xml是实现不同语言或程序之间进行数据交换的协议
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>
在Python中操作xml
import xml.etree.cElementTree as ET
terr = ET.parse('xml')
name = terr.getroot()
# print(name.tag)
#遍历xml文档
# for i in name:
# print(i.tag,i.attrib)
# for j in name:
# # print(j.tag,j.text)
#遍历rank节点
# for node in name.iter('rank'):
# print(node.tag,node.text)
修改和删除xml文档内容
import xml.etree.cElementTree as ET
terr = ET.parse('xml')
name = terr.getroot()
# print(name.tag)
#修改,把year进行加1
# for node in name.iter('year'):
# new_year = int(node.text) + 1
# node.text = str(new_year)
# node.set('update','yes')
# terr.write('xml')
#把大于2013的year删除
for country in name.findall('country'):
year = int((country.find('year')).text)
if year >= 2013:
name.remove(country)
terr.write('xml')
自建xml文档
import xml.etree.cElementTree as ET
#自建xml文档
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')
dept = ET.SubElement(name,'dept')
dept.text = 'IT'
sex.text = 'man'
age.text = '22'
name2 = ET.SubElement(new_xml,'name',attrib={'enrolled':'no'})
age = ET.SubElement(name2,'age')
sex = ET.SubElement(name2,'sex')
dept = ET.SubElement(name2,'dept')
age.text = '19'
sex.text = '女'
dept.text = 'TH'
et = ET.ElementTree(new_xml)
et.write('test.xml',encoding='utf-8',xml_declaration=True)