标签:
json和pickle都是序列化内存数据到文件
json和pickle的区别是:
如果两个程序都需要交互内存数据,如果都是python语言的情况下,复杂的交互用pickle。
用于序列化的两个模块
Json模块提供了四个功能:dumps、dump、loads、load
pickle模块提供了四个功能:dumps、dump、loads、load
dumps和dump区别:
1
2
3
4
|
pickle.dump(info,f) #print(pickle.dumps(info)) #f.write(pickle.dumps(info)) |
loads和load区别:
1
2
3
|
data_from_atm = pickle.load(f) #data_from_atm=pickle.loads(f.read()) |
pickle(python中独有,支持所有python数据类型)
将字典转化为字符串
import pickle f= open("user_acc.txt","wb") info={ "alex":"123", "jack":"4444" } f.write(pickle.dumps(info)) f.close()
将字符串转化为字典
import pickle f= open("user_acc.txt","rb") data_from_atm=pickle.loads(f.read()) print(data_from_atm)
json(是所有语言通用的,支持字典,列表,元组)
将字典转化为字符串
import pickle import json f= open("user_acc.txt","w") info={ "alex":"123", "jack":"4444" } f.write(json.dumps(info)) f.close()
将字符串转化为字典
import pickle,json f= open("user_acc.txt","r") data_from_atm=json.loads(f.read()) print(data_from_atm)
实例:
常用模块之shelve模块
shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import shelve d = shelve. open ( ‘shelve_test‘ ) #打开一个文件 class Test( object ): def __init__( self ,n): self .n = n t = Test( 123 ) t2 = Test( 123334 ) name = [ "alex" , "rain" , "test" ] d[ "test" ] = name #持久化列表 d[ "t1" ] = t #持久化类 d[ "t2" ] = t2 d.close() |
shelve和pickle的区别是:
shelve比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写,另外shelve想重复dump多少个对象都可以
常用模块之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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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) |
修改和删除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‘)
自己创建xml文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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) #打印生成的格式 |
标签:
原文地址:http://www.cnblogs.com/suliuer/p/5612173.html