json/pickle/shelve/xml/configparser/hashlib/subprocess - 总结
序列化:序列化指把内存里的数据类型转成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes
为什么要序列化:可以直接把内存数据(eg:10个列表,3个嵌套字典)存到硬盘上,下次程序再启动,再从硬盘上读回来,还是原来的格式。
用于序列化的两个模块:
json: 字符串 和python数据类型间进行转换 (dumps/dump/loads/load)
pickle: python特有的类型 和python的数据类型进行转换 (dumps/dump/loads/load)
----------------------------------------------------
json 模块
data={字典}
s = json.dumps(data) #把数据类型转成字符串
data = json.loads(s) #把字符串转成数据类型
json.dump(data,open('test.json','w',encoding='utf-8')) #把数据类型转成->字符串->存到文件里
data = json.load(open('test.json','r',encoding='utf-8')) #打开文件从->字符串->转成数据类型
dumps loads 存在的意义:1.远程传给其他人 2.不同语言之间的交互
pickle 模块
data={字典} sayhi=(函数)
s = pickle.dumps(data)
data = pickle.loads(s)
pickle.dump(data,open('test.json','wb'))
data = pickle.load(open('test.json','rb'))
s = pickle.dumps(sayhi)
data = pickle.loads(s)
pickle.dump(sayhi,open('test1.json','wb'))
pickle.load(open('test1.json','rb'))()
json和pickle区别:
json 可转化的数据类型有 int str list tuple dict 不支持set
pickle python独有的,支持python的所有数据类型,支持函数
shelve 模块
pickle封装了shelve模块,只能在python中使用,支持python的所有数据类型
#可增加 删除 可整体重新赋值
names = ["alex", "rain", "test"]
info = {'name':'alex','age':22}
f = shelve.open('shelve_test')
f["names"] = names #持久化列表
f['info_dic'] = info
f.close() d = shelve.open('shelve_test')
print(d['names'])
del d['test']
xml 模块
作用:
1.不同语言之间内存数据得交换
2.内存的数据可转换成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 = ''
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '' et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True) ET.dump(new_xml) #打印生成的格式
configparser 模块
作用:用于生成和修改常见配置文档
1.来看一个好多软件的常见配置文件格式如下
***.ini [DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no ***.cfg
[group1]
k1 = v1
k2:v2 [group2]
k1 = 2222
k2 = 1111
k3 = 'asd' [group3] ------------
import configparser
config = configparser.ConfigParser()
config.read('config.ini') print(config.sections())
print(config['bitbucket.org']['User'])
print(config['bitbucket.org']['Compression'])
print(config['topsecret.server.com']['CompressionLevel']) import configparser
config = configparser.ConfigParser()
config.read('conf.cfg') print(config.sections())
print(config.options('group2'))
print(config.items('group2'))
print(config.get('group2','k3'))
print(config.getint('group2','k1')) config.set('group2','k3','alice')
config.write(open('conf.cfg','w')) print(config.has_section('group5'))
config.add_section('group5')
config.write(open('conf.cfg','w')) config.remove_section('group5')
config.write(open('conf.cfg','w')) config.remove_option('group3','k1')
config.write(open('conf.cfg','w'))
hashlib 模块
hash (152位) 退出程序 hash() 值就变了 两个不同的变量有可能hash值是相同的
MD5 (128位) 退出程序MD5() 值不变 基于hash()的 >>> import hashlib
>>> m = hashlib.md5()
>>> m.update(b'alice')
>>> m.hexdigest() #十六进制格式的MD5
'6384e2b2184bcbf58eccf10ca7a6563c'
>>> m.digest() #二进制格式的MD5
b'\x93$(\x86\x1a\xb0\xb8\x15\x1fzP\x81H\x1eJ\x0b' MD5 功能 1.任意长度处理后都是128位 2.不同的输入得到不同的结果 唯一性
MD5 特点 1.压缩性 2.容易计算 3.抗修改性 4.强抗碰撞
MD5 用途 1.防止被篡改 2.防止直接看到明文 3.防止抵赖(数字签名) hashlib.md5() #128位
hashlib.sha1() #160位
hashlib.sha256() #256位
hashlib.sha384() #384位
hashlib.sha512() #512位
subprocess 模块
通过Python去执行一条系统命令或脚本,os.system('ls') commands popen2 等也可以
官方推出subprocess,提供统一的模块来是实现对系统命令或脚本的调用
三种执行命令的方法
subprocess.run(*popenargs, input=None, timeout=None, check=False, **kwargs) #官方推荐
subprocess.call(*popenargs, timeout=None, **kwargs) #跟上面实现的内容差不多,另一种写法
subprocess.Popen() #上面各种方法的底层封装
1.
标准写法
subprocess.run(['df','-h'],stderr=subprocess.PIPE,stdout=subprocess.PIPE,check=True) 涉及到管道|的命令需要这样写
subprocess.run('df -h|grep disk1',shell=True) #shell=True的意思是这条命令直接交给系统去执行,不需要python负责解析 subprocess.run('python test.py')
subprocess.run(['calc'],stderr=subprocess.PIPE,stdout=subprocess.PIPE,check=True) #calc mspaint tasklist
subprocess.run(['netstat','-ano'],stderr=subprocess.PIPE,stdout=subprocess.PIPE,check=True) 2.
subprocess.call('python test.py')
subprocess.call(['netstat','-ano']) #执行命令 返回执行状态 返回 0 或 非0
subprocess.check_call(['netstat','-ano']) #执行命令 结果位为0 正常返回 否则 抛异常
subprocess.getstatusoutput('dir 4.模块') #接收字符串格式命令 返回元组形式 第1个为执行状态 ,第二个为命令结果
subprocess.getoutput('dir 4.模块') #接收字符串格式命令 返回执行结果
subprocess.check_output(['netstat','-ano']) #执行命令 返回结果 3.
subprocess.Popen('python test.py')
subprocess.Popen('python3 guess_age.py',stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE,shell=True)
a=subprocess.run('sleep 10',shell=True,stdout=subprocess.PIPE)
a=subprocess.Popen('sleep 10',shell=True,stdout=subprocess.PIPE)
json/pickle/shelve/xml/configparser/hashlib/subprocess - 总结的更多相关文章
- python笔记-7(shutil/json/pickle/shelve/xml/configparser/hashlib模块)
一.shutil模块--高级的文件.文件夹.压缩包处理模块 1.通过句柄复制内容 shutil.copyfileobj(f1,f2)对文件的复制(通过句柄fdst/fsrc复制文件内容) 源码: Le ...
- 模块 - json/pickle/shelve/xml/configparser
序列化: 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes. 为什么要序列化: 有种办法可以直接把内存数据(eg:10个列表,3 ...
- PYTHON-模块 json pickle shelve xml
""" pickle 和 shevle 序列化后得到的数据 只有python才能解析 通常企业开发不可能做一个单机程序 都需要联网进行计算机间的交互 我们必须保证这个数据 ...
- Python学习笔记——基础篇【第六周】——json & pickle & shelve & xml处理模块
json & pickle 模块(序列化) json和pickle都是序列化内存数据到文件 json和pickle的区别是: json是所有语言通用的,但是只能序列化最基本的数据类型(字符串. ...
- python 序列化及其相关模块(json,pickle,shelve,xml)详解
什么是序列化对象? 我们把对象(变量)从内存中编程可存储或传输的过程称之为序列化,在python中称为pickle,其他语言称之为serialization ,marshalling ,flatter ...
- 常用模块(json/pickle/shelve/XML)
一.json模块(重点) 一种跨平台的数据格式 也属于序列化的一种方式 介绍模块之前,三个问题: 序列化是什么? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化. 反序列化又是什么? 将 ...
- python序列化及其相关模块(json,pickle,shelve,xml)详解
什么是序列化对象? 我们把对象(变量)从内存中编程可存储或传输的过程称之为序列化,在python中称为pickle,其他语言称之为serialization ,marshalling ,flatter ...
- Python模块 shelve xml configparser hashlib
常用模块1. shelve 一个字典对象模块 自动序列化2.xml 是一个文件格式 写配置文件或数据交换 <a name="hades">123</a>3. ...
- python模块(shelve,xml,configparser,hashlib,logging)
1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...
随机推荐
- django rest framework(10)
1.权限 2.认证 3.访问频率限制 4.序列化 5.路由 6.视图 7.分页 8.解析器 9.渲染器 10.版本 面试题:你写的类都继承过哪些类? class Vive(object): class ...
- C++语言基础(24)-四种类型转换运算符(static_cast、dynamic_cast、const_cast和reinterpret_cast)
一.static_cast static_cast 只能用于良性转换,这样的转换风险较低,一般不会发生什么意外,如: #include <iostream> #include <cs ...
- 0055 <mvc:default-servlet-handler />不能同时访问静态资源和动态资源的问题
刚才在折腾的时候,发现applicationContext.xml中配置了<mvc:default-servlet-handler />后,能访问静态资源,却不能访问动态资源了:注释掉之后 ...
- Java动态代理原理及其简单应用
概念 代理对象和被代理对象一般实现相同的接口,调用者与代理对象进行交互.代理的存在对于调用者来说是透明的,调用者看到的只是接口.代理对象则可以封装一些内部的处理逻辑,如访问控制.远程通信.日志.缓存等 ...
- jsp有哪些内置对象?作用分别是什么?(至少三个)
jsp有哪些内置对象?作用分别是什么?(至少三个) 解答: 1)request表示HttpServletRequest对象.它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie, head ...
- 【vijos】1768 顺序对的值(特殊的技巧)
https://vijos.org/p/1768 之前不知道为什么,我yy了一个n^2的做法,但是没能写出来..sad 然后看了题解才发现这题好神.. 为什么一定要照着题意找两个点然后算呢?这就是问题 ...
- NLM非局部均值算法相关
NLM原文: 基于图像分割的非局部均值去噪算法 基于图像分割的非局部均值去噪算法_百度文库 https://wenku.baidu.com/view/6a51abdfcd22bcd126fff705c ...
- Java反射基础(二)
获取域 1. 通过反射API可以获取到类中公开的静态域和对象中的实例域.得到表示域的java.lang.reflect.Field类的对象之后,就可以获取和设置域的值. 与获取构造方法类似,Cla ...
- 解决Ajax请求跨域问题
from:https://blog.csdn.net/wang379275614/article/details/53333775 上篇文章提到,由于浏览器的同源策略,使得,AJAX请求只能发给同源的 ...
- 组合使用QT的资源管理高级功能简化开发过程
使用 QT 进行团队开发的时候,常常碰到一个问题,就是如何共同管理资源?甚至一个人进行开发的时候如何简化资源的维护,避免无谓的消耗? 如果可以做到在开发的时候,大家把美工做的图片(往往是程序员先自己随 ...