zoukankan      html  css  js  c++  java
  • day-15时间模块,datetime模块,random随机模块

    时间模块

    python中的三种时间表现形式:
    • 1.时间戳:给电脑看的
      • 自1970-01-01 00:00:00到当前的时间,按秒计算。
    • 2.格式化时间(Format String)给人看的
      • 返回的是时间的字符串 2019-11-16
    • 3.格式化时间(struct_time):
      • 返回的是一个元组,元组中有9个值。

    补充:每个时间对一个的符号

    ​ %Y :年 Year with century as a decimal number.

    ​ %m :月 Month as a decimal number [01,12].

    ​ %d :日 Day of the month as a decimal number [01,31].

    ​ %H :小时 Hour (24-hour clock) as a decimal number [00,23].

    ​ %M :分钟 Minute as a decimal number [00,59]

    ​ %S :秒 Second as a decimal number [00,61].

    import time

    1.获取时间戳(* * * * *)计算时间是使用
    print(time.time())
    
    2.获取格式化时间 (* * * * * * *)拼接用户时间格式并保存时使用# 获取年月日
    print(time.strftime('%Y-%m-%d %H:%M:%S'))  # 2019-11-16 14:30:56
    #  %X 等同于 %H:%M:%S
    print(time.strftime('%Y-%m-%d %X'))   #2019-11-16 14:30:56
    
    3.获取时间对象 (* * * *)
    print(time.localtime())    #time.struct_time(tm_year=2019, tm_mon=11, 
    							tm_mday=16, tm_hour=14, tm_min=41, tm_sec=16, 
    							tm_wday=5, tm_yday=320, tm_isdst=0)	
    # 查看时间对象的类型
    print(type(time.localtime()))	# <class 'time.struct_time'>
    #查看时间对象中的值。
    time_obj = time.localtime()
    print(time_obj.tm_year)		#  2019
    print(time_obj.tm_hour)		#  14
    
    res = time.localtime()# 格式化时间
    time.sleep(5)
    # 获取当前时间的格式化时间
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
    
    #将时间对象转化为格式化时间
    print(time.strftime('%Y-%m-%d %H:%M:%S', res))
    
    打印结果   (演示)
    2019-11-16 14:51:52
    2019-11-16 14:51:47
    
    # 将字符串格式化时间转化为时间对象
    res = time.strptime('2019-01-01', '%Y-%m-%d')
    print(res)
    
    结果:time.struct_time(tm_year=2019, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=1, tm_isdst=-1)
    

    datetime模块

    import datetime
    # 获取当前年月日
    print(datetime.date.today())  # 2019-11-16
    # 获取当前年月日时分秒
    print(datetime.datetime.today()) # 2019-11-16 15:10:28.379538
    time_obj = datetime.datetime.today()
    # 时间类型
    print(type(time_obj))
    #打印时间的年
    print(time_obj.year)
    

    UTC时区

     UTC时区
    # 北京时间
    print(datetime.datetime.now())  # 2019-11-16 15:19:45.586179
    # 格林威治时间
    print(datetime.datetime.utcnow())  # 2019-11-16 07:19:45.586179
    

    日期/时间的计算 (*******)

    日期时间 = 日期时间 “+” or “-” 时间对象

    时间对象 = 日期时间 “+” or “-” 日期时间

    #日期时间    (当前时间)
    current_time = datetime.datetime.now()
    print(current_time)	# 2019-11-16 15:30:18.838606
    #时间对象(获取7天后的时间)
    time_obj = datetime.timedelta(days=7)
    print(time_obj)	# 7 days, 0:00:00
    #获取当前时间7天后的时间
    # 日期时间 = 日期时间 “+” or “-” 时间对象
    later_time = current_time + time_obj
    print(later_time)	# 2019-11-23 15:30:18.838606
    
    # 时间对象 = 日期时间“+”or“-”日期时间
    time_now_obj = later_time - current_time
    print(time_now_obj)   #7 days, 0:00:00
    

    random 随机模块

    • 随机获取1-9中任意值 #结果都是随机的
    res = random.randint(1, 9)
    print(res)
    
    • 默认获取0-1之间任意的小数 #结果都是随机的
    res2 = random.random()
    print(res2)
    
    • random.choice(可迭代对象):有索引的可迭代对象
    • fandom.shuffle():注意:只允许列表,注意:不可变类型不可以乱序

    洗牌,可以打乱顺序,也可以取可迭代对象的某个值

    #洗牌,将可迭代的值打乱顺序
    list1 = ['红桃A', '梅花A', '红桃Q', '方块K']
    random.shuffle(list1)
    print(list1)    #打乱他们的顺序
    #随机获取可迭代对象中的某一个值
    list1 = ['红桃A', '梅花A', '红桃Q', '方块K']
    res3 = random.choice(list1)
    print(res3)     #随机取一个
    

    需求: 随机验证码

    需求:

    ​ 大小写字母、数字组合而成

    ​ 组合5位数的随机验证码

    前置技术: - chr(97) # 可以将ASCII表中值转换成对应的字符

    ​ print(chr(101))

    ​ random.choice'''

    # 每次循环只从大小写字母,数字中取出一个字符。我们设置的验证码是5个
    # 获取任意长度的随机验证码
    def get_code(n):
        code = ''
        for i in range(n):
            #随机获取小写字母
            res = random.randint(97, 122)
            lower_str = chr(res) #返回一个Unicode字符串
            #随机获取大写字母
            res2 = random.randint(65, 90)
            upper_str = chr(res2)
            #随机获取数字
            number = str(random.randint(0, 9))
            code_list = [lower_str, upper_str, number]
            random_code = random.choice(code_list)
            code += random_code
        return code
    
    code = get_code(5)
    print(code)
    print(len(code))
    
  • 相关阅读:
    堆排序
    归并排序
    Distinct Subsequences——Leetcode
    Longest Consecutive Sequence——Leetcode
    Different Ways to Add Parentheses——Leetcode
    Haproxy 安装配置详解
    Saltstack常用模块
    SaltStack之安装
    tcpcopy复制线上流量
    nginx配置详解与优化
  • 原文地址:https://www.cnblogs.com/lishuangjian/p/11892842.html
Copyright © 2011-2022 走看看