zoukankan      html  css  js  c++  java
  • 模块一

    模块

    1.模块

    python的三种时间表现形式:

    ​ 1.时间戳:给电脑看的

    ​ -自1970-01-01到当前时间,按照秒计算,计算了多少秒

    ​ 2.格式化时间,(format string):给人看的

    ​ -返回的时间字符串 2002-01-11

    ​ 3.格式化时间对象(struct_time)

    ​ -返回的是一个元组,元组中有9个值:

    ​ -9个值分别是:年、月、日、时、分、秒 一周中的第几天,一年中的第几天,夏令时(了解)

    1.1时间戳

    #获取当前的时间
    import time
    print(time.time())
    >>>>>>>
    1573889657.3953462
    

    1.2格式化时间

    拼接用户时间,并保存时使用

    ​ 获取 年月日

    print(time.strftime('%Y-%m-%d'))
    >>>>>>
    2019-11-16
    

    -获取年月日时分秒

    print(time.strftime('%Y-%m-%d  %H:%M:%S'))####%X = %H:%M:%S 可以用这个来代替
    >>>>>>>
    2019-11-16  15:42:21
        下面是替代的例子
    print(time.strftime('%Y-%m-%d  %X'))
    >>>>>>>
    2019-11-16  15:44:25
    

    -获取时间对象

    print(time.localtime())
    print(type(time.localtime()))
    >>>>>>>>
    time.struct_time(tm_year=2019, tm_mon=11, tm_mday=16, tm_hour=15, tm_min=48, tm_sec=4, tm_wday=5, tm_yday=320, tm_isdst=0)
    <class 'time.struct_time'>######这个类型是类
    
    time_obj = time.localtime()
    print(time_obj.tm_year)
    print(time_obj.tm_mon)
    print(time_obj.tm_mday)
    print(time_obj.tm_hour)
    >>>>>>>>>>>>
    2019
    11
    16
    15
    res = time.localtime()
    #获取当前时间的格式化时间
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
    #将时间对象转化为格式化时间
    print(time.strftime('%Y-%m-%d  %H:%M:%S',res))
    >>>>>>>>
    2019-11-16 16:04:28
    2019-11-16  16:04:28
     #将字符串格式的时间转化为时间对象
    res = time.strptime('2019-11-16', '%Y-%m-%d')
    print(res)
    >>>>>>>>>>>
    time.struct_time(tm_year=2019, tm_mon=11, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=320, tm_isdst=-1)
    
    
    

    ​ -datetime

    import datetime
    #获取的当前时间的年月日
    print(datetime.date.today())
    >>>>>>>>>
    2019-11-16
    #获取当前的吧年月日时分秒
    print(datetime.datetime.today())
    >>>>>>>>>>
    2019-11-16 16:12:12.734580
            
    res = datetime.datetime.today()
    print(type(res))
    print(res.year)
    print(res.month)
    print(res.day)
    >>>>>>>>>
    2019
    11
    16
    #从索引0 开始计算一周
    print(res.weekday()) #UTC时区  0-6
    print(res.isoweekday())  #1-7
    >>>>>>>>>>>
    5
    6
    #UTC时区
    print(datetime.datetime.now())
    #格林威治时间
    print(datetime.datetime.utcnow())
    >>>>>>>>>>>
    2019-11-16 16:21:19.674298
    2019-11-16 08:21:19.674298
    
    #日期/时间的计算
    #日期时间= 日期时间 + or - 时间对象
    #日期对象= 日期时间 + or - 日期时间
    #日期时间现在的
    current_time = datetime.datetime.now()
    print(current_time)
    #时间对象 获取7天时间
    res = datetime.timedelta(days= 8)
    print(res)
    #获取当前时间8天后的时间
    #日期时间= 日期时间 + or - 时间对象
    later_time = current_time + res
    print(later_time)
    #日期对象= 日期时间 + or - 日期时间
    new_time = later_time - current_time
    print(new_time)
    >>>>>>>
    2019-11-16 16:32:06.483528
    8 days, 0:00:00
    2019-11-24 16:32:06.483528
    8 days, 0:00:00
    

    2.random:随机获取数据

    import random
     #随机获取1-9之间的值
    # res = random.randint(1,9)##这个是很重要的一点
    # print(res)
    
    #默认获取0-1之间的小数
    print(random.random())
    
    #将可迭代中的值进行乱序 不可变类型不可乱序,只能对有索引的可迭代对象乱序
    这个目前可以用的只有list
    l1 = [1,3,4,6,7,8]
    random.shuffle(l1)
    print(l1)
    #随机获取可i迭代对象中的一个值
    	random.choice(可迭代对象):注意:有索引的可迭代对象:list str tuple
        
    print(random.choice(l1))
    >>>>>>>>>
    6
    0.8089071529079167
    [3, 6, 7, 8, 4, 1]
    6
    
    
    需求: 
        大小写字母、数字组合而成
        组合5位数的随机验证码
    
    前置技术:
        - chr(97)  # 可以将ASCII表中值转换成对应的字符
        # print(chr(101))
        - random.choice
        # 获取任意长度的随机验证码
        # 随机获取一个大写字母
        # 随机获取一个小字母
        # 随机获取一个数字
    get_code =''
    		
    get_code =''
    for line in range(5):
        res1 = random.randint(97,122)###ascii中小写字母的编号
        lowwer_=chr(res1)
        res2 = random.randint(65,90)##ascii中大写字母的编号
        upper_ = chr(res2)
        number = random.randint(0,9)
        number1 = str(number)
        list1 = [lowwer_, upper_,number1]
        code1= random.choice(list1)
        get_code += code1
    print(get_code)
    >>>>>>>>
    a40Mz
    
    #下面的方法是用了函数的方法
    def func(n):
        get_code = ''
        for line in range(n):
            res1 = random.randint(97, 122)
            lowwer_ = chr(res1)
            res2 = random.randint(65, 90)
            upper_ = chr(res2)
            number = random.randint(0, 9)
            number1 = str(number)
            list1 = [lowwer_, upper_, number1]
            code1 = random.choice(list1)
            get_code += code1
        print(get_code)
        return get_code
    func(6)
    

    3.OS

    os是与操作系统交互的模块

    import os
    #获取当前项目的根目录
    #获取当前的文件中的上一级目录
    
    DAY_16PATH = os.path.dirname(__file__)
    print(DAY_16PATH)
    >>>>>>>
    E:/PycharmProjects/study
    
    #项目的根目录,路径相关的值都用“常量”
    #常量用的纯大写的字母来表示
    BASE_PATH = os.path.dirname(DAY_16PATH)##这个是求的根目录的路												径
    print(BASE_PATH)
    >>>>>>>>
    E:/PycharmProjects 
        
    DAY_16PATH = os.path.dirname(__file__)
    print(DAY_16PATH)
    
    TEST_PATH = os.path.join(DAY_16PATH,'name.txt')
    print(TEST_PATH)
    
    #判断文件/文件夹是否存在,若文件存在 返回True 不存在返回False
    print(os.path.exists(TEST_PATH))
    print(os.path.exists(DAY_16PATH))
    >>>>>>>>>
    True
    True
    #判断文件夹是否存在
    print(os.path.isdir(TEST_PATH))
    print(os.path.isdir(DAY_16PATH))
    False
    True
    
    # 文件的拼接,拼接的绝对路径   老男孩.txt 这个是新起的名字
    MK_PATH = os.path.join(DAY_16PATH,'老男孩.txt')
    
    #创造了一个新的文件夹 老男孩.txt
    os.mkdir(MK_PATH)
    #删除文件 只能删除空的文件夹
    os.rmdir(MK_PATH)
    # 判断文件是否存在
     os.path.isfile()
    
    
    #获取某个文件夹里面所有文件的名字
    oldboy_list =os.listdir(r'E:PycharmProjectsstudy老男孩写真集')
    print(oldboy_list)
    
    
    import os
    #获取某个文件夹里面所有文件的名字
    oldboy_list =os.listdir(r'E:PycharmProjectsstudy老男孩写真集')
    print(oldboy_list)
    #enumerate(可迭代对象)—— >得到一个个对象,是元组(索引,元素)
    res = enumerate(oldboy_list)
    print(res)
    # #让用户选择文件
    while True:
        for index, name in enumerate(oldboy_list):#打印老师们的所有作品
            print(f'编号:{index}  文件名:{name}')  #拼接文件
        choice = input('你想看的文件夹:').strip(' ')
        #限制客户必须写数字,并且书自必须在编号范围内
        #如果不是数字,需要重新选择
        if not choice.isdigit():
            print('必须输入数字')
            continue
        #若是数字,往下走判断是否在编号范围内
        choice = int(choice)
    
        #判断如果不在列表范围内,则重新选择
        if choice not in range(len(oldboy_list)):
            print('编号范围错误')
        file_name = oldboy_list[choice]
        # print(file_name)
        OLDBOY_PATH = os.path.join(r'E:PycharmProjectsstudy老男孩写真集',file_name)
        print(OLDBOY_PATH)
        with open(OLDBOY_PATH, 'r',encoding = 'utf-8')as f:
            print(f.read())
    
    

    4.SYS

    与python解释器交互的模块

    import sys
    import os
    
    # 获取当前的Python解释器的环境变量路径
    print(sys.path)
    
    # 将当前项目添加到环境变量中
    BASE_PATH = os.path.dirname(os.path.dirname(__file__))
    sys.path.append(BASE_PATH)
    
    # 获取cmd终端的命令行  python3 py文件 用户名 密码
    print(sys.argv)  # 返回的是列表['']
    
    

    5.hashlib

    加密的模块

    MD5 不可解密的算法,这个是 在(2018年之前的)

    ​ -摘要算法:

    ​ -摘要从某个内容中获取加密的字符串

    ​ -摘要一样,内容就一定 一样,保证唯一性

    ​ -密文的密码就是一个摘要

    import hashlib
    md5_obj = hashlib.md5()
    print(hashlib.md5())   #获取的是一个对象的地址
    str1 = '1234'
    #update中一定要传入bytes的类型的数据
    md5_obj.update(str1.encode('utf-8'))
    #得到一个加密的字符串
    res = md5_obj.hexdigest()
    print(res) 
    #>>>>>81dc9bdb52d04dc20036dbd8313ed055   ###这个就是加密的密码
    with open(r'md5.txt','r',encoding = 'utf-8') as f:
        user_str = f.read()
        
        ##下面是模拟一个客户输入加密的密码登录的操作
        
        user_str1 = f'david:1234'
    user_str2 = f'david:{res}'
    with open(r'md5.txt','w',encoding = 'utf-8') as f:
        f.write(user_str2)
    
    #获取文件的用户名和方法
    with open(r'md5.txt','r',encoding = 'utf-8') as f:
        user_str = f.read()
        
        
    #用户输入密码
    file_str, file_pwd = user_str.split(':')
    user_name = input('请输入你的名字:').strip()
    user_pwd = input('请输入你的密码:').strip()
    
    md5_1 =hashlib.md5()
    md5_1.update(user_pwd.encode('utf-8'))
    sal1 = '好帅的david'
    md5_1.update(sal1.encode('utf-8'))
    res = md5_1.hexdigest()
    print(res)
    
    #校验密码和用户名是不是一致
    if file_str == user_name and file_pwd == res:
        print('登录成功')
    else:
        print('登录失败')
        
        
        
        下面这个是用函数的方法来操作
        import hashlib
    
    def pwd_md5(pwd):
        md5_obj = hashlib.md5()
        print(md5_obj)
        str = pwd
        md5_obj.update(str.encode('utf-8'))
        sal = '好帅的david'#一定要加盐
        md5_obj.update(sal.encode('utf-8'))
        res = md5_obj.hexdigest()
        print(res)
        return res
    # # pwd = '1234'
    # # pwd_md5(pwd)
    # res = '122345'
    user_str1 = f'david:1234'
    user_str2 = f'david:{res}'
    with open(r'md5.txt','w',encoding = 'utf-8') as f:
        f.write(user_str2)
    
    #获取文件的用户名和方法
    with open(r'md5.txt','r',encoding = 'utf-8') as f:
        user_str = f.read()
    #用户输入密码
    file_str, file_pwd = user_str.split(':')
    user_name = input('请输入你的名字:').strip()
    user_pwd = input('请输入你的密码:').strip()
    
    # md5_1 =hashlib.md5()
    # md5_1.update(user_pwd.encode('utf-8'))
    # sal1 = '好帅的david'
    # md5_1.update(sal1.encode('utf-8'))
    # res = md5_1.hexdigest()
    # print(res)
    
    #校验密码和用户名是不是一致
    if file_str == user_name and file_pwd == pwd_md5(user_pwd):
        print('登录成功')
    else:
        print('登录失败')
        >>>>>>>>>>>>>>>>>
    请输入你的名字:david
    请输入你的密码:1234
    <md5 HASH object @ 0x00000211383ED8F0>
    1d1b524a1fb5bce6d459ed52976ae4fa
    登录成功
    
    

  • 相关阅读:
    轻院:2211: 小明的有趣回文数
    轻院:2209: 小明找整数
    轻院:2206: 小明发福利
    轻院:2207: 小明被提问
    2135: 这里是天堂!
    牛客小白月赛2:文
    轻院2261: flower
    问题 I: 堆
    SharePoint Server 2010安装图解
    Microsoft Windows Sharepoint Services V3.0 安装图示
  • 原文地址:https://www.cnblogs.com/bs2019/p/11889273.html
Copyright © 2011-2022 走看看