模块 序列化 json pickle shelv xml

Posted ham_731

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模块 序列化 json pickle shelv xml相关的知识,希望对你有一定的参考价值。

序列化

序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受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) #打印生成的格式

以上是关于模块 序列化 json pickle shelv xml的主要内容,如果未能解决你的问题,请参考以下文章

常用文件操作模块json,pickle和shelve

序列化模块 json pickle shelve

json模块pickle模块(序列化)shelve模块

什么是序列化, pickle, shelve(春节再整理), json, configparser(春节再整理)模块

json模块,pickle模块,shelve模块

常用模块(json/pickle/shelve/XML)