shelve,xml,configparser,hashlib
admin
2023-07-15 19:22:23
0

#shelve模块

import shelve
d=shelve.open('shelve_test')

t = Tes(123)
t2 = Tes(123334)

name = ["alex", "rain", "test"]
d["test"] = name  # 持久化列表
d["t1"] = t  # 持久化类
d["t2"] = t2

d.close()
#写
import shelve,datetime
d = shelve.open('shelve_test')
info = {'age':22,'job':'it'}
name = ['alex','rain','test']
d['name']=name
d['info']=info
d['date']=datetime.datetime.now()
d.close()

#读
import shelve
d = shelve.open('shelve_test')
print(d.get('name'))
print(d.get('info'))
print(d.get('date'))

 

#xml模块


xmltes.xml


 version="1.0"?>

     name="Liechtenstein">

         updated="yes">2

        2008

        141100

         name="Austria" direction="E"/>

         name="Switzerland" direction="W"/>

    

     name="Singapore">

         updated="yes">5

        2011

        59900

         name="Malaysia" direction="N"/>

    

     name="Panama">

         updated="yes">69

        2011

        13600

         name="Costa Rica" direction="W"/>

         name="Colombia" direction="E"/>

    


import xml.etree.ElementTree as ET
tree = ET.parse("xmltes.xml")
root=tree.getroot()
#print(root) #文档内存地址
#print(root.tag) #根的名 标签名

#遍历 xml文档
for  child in root:
    print(child.tag,child.attrib)
    for i in child:
        print(i.tag,i.text,i.attrib #attrib属性 text内容
        

#只遍历year节点
for node in root.iter('year'):
    print(node.tag,node.text)

#修改xml文档
for node in root.iter('year'):
    new_year = int(node.text) + 1
    node.text=str(new_year)
    node.set("updated","yes") #修改属性

tree.write("xmltes.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
new_xml=ET.Element("personinfolist")
personinfo=ET.SubElement(new_xml,'personinfo',attrib={"enrolled":"yes"})
name=ET.SubElement(personinfo,"name")
name.text="alex"
age=ET.SubElement(personinfo,'age',attrib={"checked":"no"})
sex=ET.SubElement(personinfo,"sex")
age.text="88"
personinfo2=ET.SubElement(new_xml,'personinfo',attrib={"enrolled":"no"})
name=ET.SubElement(personinfo2,"name")
name.text="haha"
age=ET.SubElement(personinfo2,"age")
age.text='19'

et=ET.ElementTree(new_xml) #生成文档对象
et.write("tes.xml",encoding="utf-8",xml_declaration=True)
ET.dump(new_xml)#打印生成的格式

 

Configparser模块

#用于生成和修改常见配置文档
import configparser
#生成配置文件
config=configparser.ConfigParser()
config["DEFAULT"]={'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9'}

config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example','w') as configfile:
    config.write(configfile)
#读配置文件
conf=configparser.ConfigParser()
conf.read("example")
print(conf.defaults())
print(conf['bitbucket.org']['user'])    #section部分

#删除section

conf.read("example")

sec = conf.remove_section('bitbucket.org') #section部分 删除bitbucket.org部分
conf.write(open('example.cfg','w')) #写入 可修改原文件

#判断section部分

conf.read("example")

print(conf.has_section("topsecret.server.com")) #存在则为true

#添加section

conf.read("example")

sec2=conf.add_section("HOST")

conf.write(open('example.cfg','w'))


#修改section下的key value
conf.read("example.cfg")

conf.set('HOST','hostname_ip','192.168.0.1')  #conf.set(section,'key','value')
conf.write(open('example.cfg','w'))

 

#hashlib模块

import hashlib
#用于加密相关的操作
m = hashlib.md5()
m.update(b'hello') #update叠加 获取hello的md5值
print(m.hexdigest()) #十六进制 输出并打印hello的md5值
m.update(b"It's me")
print(m.hexdigest())
m.update(b"It's been a long time since last time we ...")
print(m.hexdigest())

print('----------------------------------------')

m2=hashlib.md5()
m2.update(b"helloIt's me")  #拼起来
print(m2.hexdigest())


s2=hashlib.sha256()
s2.update(b"helloIt's me")
print(s2.hexdigest())

import hmac
#对我们创建 key 和 内容 再进行处理然后再加密 用于消息加密
h = hmac.new('天王盖地虎'.encode(encoding="utf-8"),'宝塔镇河妖'.encode(encoding='utf-8'))
print(h.digest()) #十进制
print(h.hexdigest())

相关内容

热门资讯

特朗普:正致力于与伊朗达成协议... 特朗普在《纽约邮报》一档播客访谈节目中称,他正与伊朗磋商一项协议,伊朗已同意不再谋求拥有核武器。他表...
不接壤的日菲为何偷划海界? 日菲近日发表联合声明,宣称就“划定两国专属经济区和大陆架的海洋边界”启动正式谈判。两个隔海相望的国家...
凤凰晚报丨从钳工到老戏骨,魏宗... 今日人物【从钳工到老戏骨,魏宗万用一生诠释“戏比天大”】6月1日,表演艺术家魏宗万在上海逝世,享年8...
科威特称伊朗袭击致63人受伤 科威特卫生部门3日称,伊朗当天对科威特的袭击已造成63人受伤,相关部门已启动紧急应对预案,并在全国范...
日本标榜“和平国家”却行扩军备... 今年是东京审判开庭80周年,世界正回望历史、反思战争罪责、捍卫二战后来之不易的国际秩序之际,日本却迈...
浙江杨梅即将大规模上市,如何破... “我们现在的压力很大。”5月底,浙江余姚杨梅产区丈亭镇副镇长林宇站在一片杨梅林前对第一财经表示,当地...
致5死2伤!韩国就韩华航空航天... 【环球网报道 记者 姜蔼玲】据韩联社6月1日报道,针对位于韩国大田的韩华航空航天公司发生爆炸致7人伤...
黄河科技学院2026年招生简章 长按图片识别二维码或点击 “阅读原文” 查看电子招生简章。
医路起航,从“心” 开始!黄河... 6月1日上午,黄河科技学院附属医院2022级临床医学本科实习生入院岗前培训在大医讲堂顺利举办。院领导...
问题居然在实体卡槽上!美版iP... 6月2日消息,日前,又有博主提前把还没发布的iPhone 18 Pro电池参数给曝光了出来,根据爆料...