设为首页 加入收藏

TOP

Python-14-常用模块(三)
2019-07-15 16:09:59 】 浏览:214
Tags:Python-14- 常用 模块
f.write(j) # 等价于json.dump(dic,f) f.close() #-----------------------------反序列化 import json f=open('序列化对象') data=json.loads(f.read())# 等价于data=json.load(f)
import json
#dct="{'1':111}"#json 不认单引号
#dct=str({"1":111})#报错,因为生成的数据还是单引号:{'one': 1}

dct='{"1":"111"}'
print(json.loads(dct))

#conclusion:
#        无论数据是怎样创建的,只要满足json格式,就可以json.loads出来,不一定非要dumps的数据才能loads
注意

3. pickle模块

pickle模块的用法与json相同,但转化的不是字符串类型,而是bytes类型。

Pickle的问题和所有其他编程语言特有的序列化问题一样,就是它只能用于Python,并且可能不同版本的Python彼此都不兼容,因此,只能用Pickle保存那些不重要的数据,不能成功地反序列化也没关系。

import pickle

dic = {'name': 'alvin', 'age': 23, '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()

# -------------------------反序列化
f = open('序列化对象_pickle', 'rb')
data = pickle.loads(f.read())  # 等价于data=pickle.load(f)
print(data['age'])

六、shelve模块

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

import shelve

f = shelve.open(r'shelve.txt')  # 将一个字典放入文本 f = {}
f['stu1'] = 'Tom'
f['stu2'] = {'name': 'gangdan', 'age': 53}
f['school_info'] = {'website': 'http://www.pypy.org', 'city': 'beijing'}

print(f['stu1'])
print(f['stu2']['name'])
f.close()

 七、xml模块

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml,至今很多传统公司如金融行业的很多系统的接口还主要是xml。

 1 import xml.etree.ElementTree as ET
 2 
 3 tree = ET.parse("xml")
 4 root = tree.getroot()
 5 
 6 # xml遍历
 7 for i in root:
 8     # print(i.tag)
 9     # print(i.attrib)
10     for j in i:
11         print(j.tag)  # 标签
12         print(j.attrib)  # 属性
13         print(j.text)  # 文本
14 
15 # 只遍历year节点
16 for node in root.iter('year'):
17     print(node.tag, node.text)
18 # year 2008
19 # year 2011
20 # year 2011
21 
22 # 修改
23 for node in root.iter('year'):
24     # 修改文本
25     new_year = int(node.text) + 1
26     node.text = str(new_year)
27     # 修改属性
28     node.set('updated', 'yes')
29 # 写入修改
30 tree.write('new.xml')
31 
32 # 在country内添加(append)节点year2
33 for country in root.findall('country'):
34     for year in country.findall('year'):
35         if int(year.text) > 2000:
36             year2 = ET.Element('year2')
37             year2.text = '新年'
38             year2.attrib = {'update': 'yes'}
39             country.append(year2)  # 往country节点下添加子节点
40 
41 tree.write('a.xml.swap')
42 
43 # 删除
44 for country in root.findall('country'):
45     rank = int(country.find('rank').text)
46     if rank > 50:
47         root.remove(country)
48 tree.write('new.xml')
常用操作
 1 # 创建xml文件
 2 import xml.etree.ElementTree as ET
 3 
 4 new_xml = ET.Element("namelist")
 5 name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
 6 age = ET.SubElement(name, "age", attrib={"checked": "no"})
 7 sex = ET.SubElement(name, "sex")
 8 sex.text = '33'
 9 name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
10 age = ET.SubElement(name2, "age")
11 age.text = '19'
12 
13 et = ET.ElementTree(new_xml)  # 生成文档对象
14 et.write("test.xml", encoding="utf-8", x
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 3/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇用Python递归做个多层次的文件执行 下一篇数据库-高级部分

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目