欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

从零开始的Python学习Episode 17——序列化

程序员文章站 2023-02-15 10:29:01
序列化 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语 言中也被称之为serialization,marshalling,flattening等等,都是一个意思。 序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上。 ......

序列化

我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在python中叫pickling,在其他语

言中也被称之为serialization,marshalling,flattening等等,都是一个意思。

序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上。

反过来,把变量内容从序列化的对象重新读到内存里称之为反序列化,即unpickling。

 

json

如果我们要在不同的编程语言之间传递对象,就必须把对象序列化为标准格式,比如xml,但更好

的方法是序列化为json,因为json表示出来就是一个字符串,可以被所有语言读取,也可以方便

地存储到磁盘或者通过网络传输。json不仅是标准格式,并且比xml更快,而且可以直接在web

页面中读取,非常方便。

json表示的对象就是标准的javascript语言的对象,json和python内置的数据类型对应如下:

从零开始的Python学习Episode 17——序列化

# ----------------------------序列化
import json

dic = {'name': 'pp', 'age': 20, 'sex': 'male'}
print(type(dic))  # <class 'dict'>

j = json.dumps(dic)
print(type(j))  # <class 'str'>

f = open('序列化对象', 'w')
f.write(j)  # -------------------等价于json.dump(dic,f)
f.close()
# -----------------------------反序列化
import json

f = open('序列化对象')
data = json.loads(f.read())  # 等价于data=json.load(f)
print(data) # {'name': 'pp', 'age': 20, 'sex': 'male'}

 

pickle

##----------------------------序列化
import pickle

dic = {'name': 'pp', 'age': 20, 'sex': 'male'}

print(type(dic))  # <class 'dict'>

j = pickle.dumps(dic)
print(type(j))  # <class 'bytes'>

f = open('序列化对象_pickle', 'wb')  # 注意是w是写入str,wb是写入bytes,j是'bytes'
f.write(j)  # -------------------等价于pickle.dump(dic,f)

f.close()
# -------------------------反序列化
import pickle

f = open('序列化对象_pickle', 'rb')

data = pickle.loads(f.read())  # 等价于data=pickle.load(f)

print(data['age'])

pickle只能用于python,并且可能不同版本的python彼此都不兼容,因此,只能用pickle保存那些不重要的数据,不能成功地反序列化也没关系。

 

shelve

 shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型

import shelve

f = shelve.open(r'shelve.txt')

f['stu1_info']={'name':'pp','age':'18'}
f['stu2_info']={'name':'pp2','age':'20'}

print(f.get('stu1_info')['name'])
f.close()

 

xml

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,在json还没诞生的时候,只能用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>

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)
#---------------------------------------

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')

自己创建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) #打印生成的格式