zoukankan      html  css  js  c++  java
  • day23-常用模块二

    一、json与pickle模块

    1、什么是序列化&反序列化
    内存中的数据类型---->序列化---->特定的格式(json格式或者pickle格式)
    内存中的数据类型<----反序列化<----特定的格式(json格式或者pickle格式)

    土办法:
    {'aaa':111}--->序列化str({'aaa':111})----->"{'aaa':111}"
    {'aaa':111}<---反序列化eval("{'aaa':111}")<-----"{'aaa':111}"

    2、为何要序列化
    序列化得到结果=>特定的格式的内容有两种用途
    1、可用于存储=》用于存档
    2、传输给其他平台使用=》跨平台数据交互
    python java
    列表 特定的格式 数组

    强调:
    针对用途1的特定一格式:可是一种专用的格式=》pickle只有python可以识别
    针对用途2的特定一格式:应该是一种通用、能够被所有语言识别的格式=》json


    3、如何序列化与反序列化

    示范1
    import json
    # 序列化
    json_res=json.dumps([1,'aaa',True,False])
    # print(json_res,type(json_res)) # "[1, "aaa", true, false]"
    
    # 反序列化
    l=json.loads(json_res)
    print(l,type(l))
    
    
    示范2:
    import json
    
    序列化的结果写入文件的复杂方法
    json_res=json.dumps([1,'aaa',True,False])
    # print(json_res,type(json_res)) # "[1, "aaa", true, false]"
    with open('test.json',mode='wt',encoding='utf-8') as f:
        f.write(json_res)
    
    将序列化的结果写入文件的简单方法
    with open('test.json',mode='wt',encoding='utf-8') as f:
        json.dump([1,'aaa',True,False],f)
    
    
    从文件读取json格式的字符串进行反序列化操作的复杂方法
    with open('test.json',mode='rt',encoding='utf-8') as f:
        json_res=f.read()
        l=json.loads(json_res)
        print(l,type(l))
    
    从文件读取json格式的字符串进行反序列化操作的简单方法
    with open('test.json',mode='rt',encoding='utf-8') as f:
        l=json.load(f)
        print(l,type(l))
    
    
    json验证: json格式兼容的是所有语言通用的数据类型,不能识别某一语言的所独有的类型
    json.dumps({1,2,3,4,5})
    
    json强调:一定要搞清楚json格式,不要与python混淆
    l=json.loads('[1, "aaa", true, false]')
    l=json.loads("[1,1.3,true,'aaa', true, false]")
    print(l[0])

    二、configparser模块

    import configparser
    
    config=configparser.ConfigParser()
    config.read('test.ini')
    
    1、获取sections
    print(config.sections())
    
    2、获取某一section下的所有options
    print(config.options('section1'))
    
    3、获取items
    print(config.items('section1'))
    
    4、
    res=config.get('section1','user')
    print(res,type(res))
    
    res=config.getint('section1','age')
    print(res,type(res))
    
    
    res=config.getboolean('section1','is_admin')
    print(res,type(res))
    
    
    res=config.getfloat('section1','salary')
    print(res,type(res))

    了解

    l = json.loads(b'[1, "aaa", true, false]')
    print(l, type(l))
    
    with open('test.json',mode='rb') as f:
        l=json.load(f)
    
    
    res=json.dumps({'name':'哈哈哈'})
    print(res,type(res))
    
    res=json.loads('{"name": "u54c8u54c8u54c8"}')
    print(res,type(res))

    4、猴子补丁
    在入口处打猴子补丁

    import json
    import ujson
    
    def monkey_patch_json():
        json.__name__ = 'ujson'
        json.dumps = ujson.dumps
        json.loads = ujson.loads
    
    monkey_patch_json() # 在入口文件出运行
    
    
    import ujson as json # 不行
    
    后续代码中的应用
    json.dumps()
    json.dumps()
    json.dumps()

     5.pickle模块

       import pickle
     res=pickle.dumps({1,2,3,4,5})
     print(res,type(res))
    
     s=pickle.loads(res)
     print(s,type(s))

    三、hashlib模块

    1、什么是哈希hash
    hash一类算法,该算法接受传入的内容,经过运算得到一串hash值
    hash值的特点:
    I 只要传入的内容一样,得到的hash值必然一样
    II 不能由hash值返解成内容
    III 不管传入的内容有多大,只要使用的hash算法不变,得到的hash值长度是一定

    2、hash的用途
    用途1:特点II用于密码密文传输与验证
    用途2:特点I、III用于文件完整性校验

    3、如何用

    import hashlib
    
    m=hashlib.md5()
    m.update('hello'.encode('utf-8'))
    m.update('world'.encode('utf-8'))
    res=m.hexdigest() # 'helloworld'
    print(res)
    
    m1=hashlib.md5('he'.encode('utf-8'))
    m1.update('llo'.encode('utf-8'))
    m1.update('w'.encode('utf-8'))
    m1.update('orld'.encode('utf-8'))
    res=m1.hexdigest()# 'helloworld'
    print(res)

    模拟撞库

    cryptograph='aee949757a2e698417463d47acac93df'
    import hashlib
    
    # 制作密码字段
    passwds=[
        'alex3714',
        'alex1313',
        'alex94139413',
        'alex123456',
        '123456alex',
        'a123lex',
    ]
    
    dic={}
    for p in passwds:
        res=hashlib.md5(p.encode('utf-8'))
        dic[p]=res.hexdigest()
    
    # 模拟撞库得到密码
    for k,v in dic.items():
        if v == cryptograph:
            print('撞库成功,明文密码是:%s' %k)
            break

    提升撞库的成本=>密码加盐

    import hashlib
    
    m=hashlib.md5()
    
    m.update('天王'.encode('utf-8'))
    m.update('alex3714'.encode('utf-8'))
    m.update('盖地虎'.encode('utf-8'))
    print(m.hexdigest())

    四、subprocess模块

    import subprocess
    
    obj=subprocess.Popen('echo 123 ; ls / ; ls /root',shell=True,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE,
                     )
    
    # print(obj)
    # res=obj.stdout.read()
    # print(res.decode('utf-8'))
    
    err_res=obj.stderr.read()
    print(err_res.decode('utf-8'))
     1 import  subprocess
     2 
     3 '''
     4 sh-3.2# ls /Users/egon/Desktop |grep txt$
     5 mysql.txt
     6 tt.txt
     7 事物.txt
     8 '''
     9 
    10 res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE)
    11 res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout,
    12                  stdout=subprocess.PIPE)
    13 
    14 print(res.stdout.read().decode('utf-8'))
    15 
    16 
    17 #等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
    18 res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE)
    19 print(res1.stdout.read().decode('utf-8'))
    20 
    21 
    22 #windows下:
    23 # dir | findstr 'test*'
    24 # dir | findstr 'txt$'
    25 import subprocess
    26 res1=subprocess.Popen(r'dir C:UsersAdministratorPycharmProjects	est函数备课',shell=True,stdout=subprocess.PIPE)
    27 res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
    28                  stdout=subprocess.PIPE)
    29 
    30 print(res.stdout.read().decode('gbk')) #subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码
  • 相关阅读:
    学习链接
    【转】C#学习路线WinForm学习路线
    WPF 等待动画控件
    wpf 设置窗体在屏幕的初始位置
    WPF 添加右键菜单 自定义透明控件
    记录自己的点滴
    Linux下的mysql默认大小写敏感
    springboot+mybatis遇到BUG:自动注入失败
    springboot在阿里CentOS 7后台永久运行
    阿里云CentOS7 64位安装jdk8和mysql5.6.43及远程连接mysql
  • 原文地址:https://www.cnblogs.com/xiao-zang/p/12608470.html
Copyright © 2011-2022 走看看