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 = '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) #打印生成的格式

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$(x86x1axb0xb8x15x1fzPx81Hx1eJx0b'

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)
原文地址:https://www.cnblogs.com/alice-bj/p/8474236.html