• hashlib模块, shelve模块,xml文件解析,configparser模块


    hashlib模块

    '''
    1 什么是hash
        hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值
        如果把hash算法比喻为一座工厂
        那传给hash算法的内容就是原材料
        生成的hash值就是生产出的产品
    
    2、为何要用hash算法
        hash值/产品有三大特性:
            1、只要传入的内容一样,得到的hash值必然一样
            2、只要我们使用的hash算法固定,无论传入的内容有多大,
                得到的hash值的长度是固定的
            3、不可以用hash值逆推出原来的内容
    
            基于1和2可以在下载文件时做文件一致性校验
            基于1和3可以对密码进行加密
    
    3、如何用
    '''
    import hashlib
    
    # #1、造出hash工厂
    # m=hashlib.md5()
    #
    # #2、运送原材料
    # m.update('你好啊美丽的'.encode('utf-8'))
    # m.update('张铭言'.encode('utf-8'))
    #
    # #3、产出hash值
    # print(m.hexdigest()) #66bcb9758826f562ae8cb70d277a4be9
    
    
    # #1、造出hash工厂
    # m=hashlib.md5('你'.encode('utf-8'))
    #
    # #2、运送原材料
    # m.update('好啊美丽的张铭言'.encode('utf-8'))
    #
    # #3、产出hash值
    # print(m.hexdigest()) #66bcb9758826f562ae8cb70d277a4be9
    
    
    
    # 应用一:文件一致性校验
    # #1、造出hash工厂
    # m=hashlib.sha512('你'.encode('utf-8'))
    #
    # #2、运送原材料
    # m.update('好啊美sadfsadf丽asdfsafdasdasdfsafsdafasdfasdfsadfsadfsadfsadfasdff的张铭言'.encode('utf-8'))
    #
    #
    # #3、产出hash值
    # print(m.hexdigest()) #2ff39b418bfc084c8f9a237d11b9da6d5c6c0fb6bebcde2ba43a433dc823966c
    
    
    # #1、造出hash工厂
    # m=hashlib.md5()
    #
    # #2、运送原材料
    # with open(r'E:1.mp4','rb') as f:
    #     for line in f:
    #         m.update(line)
    # #3、产出hash值
    # print(m.hexdigest()) #1273d366d2fe2dc57645cc1031414a05
    # #                     1273d366d2fe2dc57645cc1031414a05
    
    
    # 应用一:对明文密码进行加密
    # password=input('>>>: ')
    #
    # m=hashlib.md5()
    # m.update('天王盖地虎'.encode('utf-8'))
    # m.update(password.encode('utf-8'))
    # print(m.hexdigest()) #95bd6eafefdf51d8b153785f3fb6263d
    #
    
    
    import hmac
    
    m=hmac.new('小鸡炖蘑菇'.encode('utf-8'))
    m.update('hello'.encode('utf-8'))
    print(m.hexdigest())
    View Code

    shelve模块

    import shelve
    
    # dic1={'pwd':'alex3714','age':18,'sex':'male'}
    # dic2={'pwd':'alex3715','age':73,'sex':'male'}
    
    d=shelve.open('db.txt',writeback=True)
    # # d['egon']=dic1
    # # d['alex']=dic2
    # d['egon']['age']=19
    print(d['egon'])
    d.close()
    View Code

    xml文件解析

    import xml.etree.ElementTree as ET
    
    tree = ET.parse("a.xml")
    root = tree.getroot()
    
    # 对于任何标签都有三个特征:标签名、标签属性、标签的文本内容
    # print(root.tag)
    # print(root.attrib)
    # print(root.text)
    
    # print(list(root.iter('year'))) #全文搜索,找到所有
    # for year in root.iter('year'):
    #     print(year.tag)
    #     print(year.attrib)
    #     print(year.text)
    #     print('='*100)
    
    
    # print(root.find('country').attrib) #在root的子节点找,只找一个
    # print([country.attrib for country in root.findall('country')]) #在root的子节点找,找所有
    
    
    # 1、查
    #遍历整个文档
    # for country in root:
    #     print('============>国家 %s' %country.attrib)
    #     for item in country:
    #         print(item.tag)
    #         print(item.attrib)
    #         print(item.text)
    
    #2、改
    # for year in root.iter('year'):
    #     print(year.tag)
    #     year.attrib={'updated':'yes'}
    #     year.text=str(int(year.text)+1)
    #
    # tree.write('a.xml')
    
    #3、增
    # for country in root:
    #     rank=country.find('rank')
    #     if int(rank.text) > 50:
    #         # print('符号条的国家',country.attrib)
    #         tag=ET.Element('egon')
    #         tag.attrib={'updated':'yes'}
    #         tag.text='NB'
    #         country.append(tag)
    #
    # tree.write('a.xml')
    #4、删
    
    # for country in root:
    #     tag=country.find('egon')
    #     # print(tag,bool(tag))
    #     if tag is not None:
    #         print('====>')
    #         country.remove(tag)
    # tree.write('a.xml')
    View Code
    # tree = ET.parse('a.xml')  # 打开一个xml的文件
    # root = tree.getroot()   # 用getroot 拿到树根
    #
    # print(root.tag)  # 标签名
    # print(root.attrib)  # 标签属性
    # print(root.text)  # 文本内容
    #
    #
    # print(root.iter('year'))
    # for year in root.iter('year'):
    #     print(year.tag)
    #     print(year.attrib)
    #     print(year.text)
    #     print('='*100)
    #
    #
    # print(root.find('country').attrib)
    # print([country.attrib for country in root.findall('country')])
    
    # root.iter('year')   #全文搜索
    # root.findall('country')  # 在root的子节点找,找所有
    # root.find('country')  # 在root的子节点找,只找一个
    xml文件解析补充

    configparser模块

    import configparser
    
    config=configparser.ConfigParser()
    config.read('config.ini') #a.cfg a.ini a.cnf
    
    # print(config.sections())
    # print(config.options('egon'))
    # print(config.items('egon'))
    
    
    # res=config.get('egon','age')
    # res=config.getint('egon','age')
    # print(res,type(res))
    
    # res=config.getfloat('egon','salary')
    # print(res,type(res))
    
    # res=config.getboolean('egon','is_beautiful')
    # print(res,type(res))
    configparser模块
  • 相关阅读:
    生物信息学常用的在线网站及数据库汇总
    Python入门--6--今天抄袭人家一篇日志--numpy这个
    Perl语言--一些关于赋值、引用的东西
    Python入门--5--列表
    Idea下 调试Spring源代码
    Java OOM出现场景
    分布式事务解决方案
    mybatis generator 根据表生成对应的mapper文件
    签名工具类
    BPMN2.0
  • 原文地址:https://www.cnblogs.com/xiejintao0914/p/9225153.html
Copyright © 2020-2023  润新知