• Python常用模块(1)—— time、datetime、randrom


    time 模块与时间相关的功能的模块

    在 Python 中,时间分为三种:

      1、时间戳:是一个时间的表示,根据不同的语言,可以是整数或浮点数,是从1970年1月1日0时0分0秒到现在经历的秒数

      2、UTC时间: 又称为世界协调时间,以英国的格林尼治天文所在地区的时间作为参考的时间,也叫做世界标准时间。中国时间是 UTC+8 东八区

      3、localtime:本地时间,又叫时间元组,是一个包含时间内容的普通元组

    import time
    print(time.localtime())
    
    # 运行
    time.struct_time(tm_year=2018, tm_mon=10, tm_mday=15, tm_hour=14, tm_min=44, tm_sec=34, tm_wday=0, tm_yday=288, tm_isdst=0)
    
    
    # 相应参数
    # 索引     内容        属性            值
    #  0       年        tm_year         2015
    #  1       月        tm_mon          1~12
    #  2       日        tm_mday         1~31
    #  3       时        tm_hour         0~23
    #  4       分        tm_min          0~59
    #  5       秒        tm_sec          0~61  60表示闰秒  61保留值
    #  6       周几      tm_wday         0~6
    #  7       第几天    tm_yday          1~366
    #  8       夏令时    tm_isdst         0,1,-1(表示夏令时)
    localtime

    时间模块的属性

    # 获取时间戳  返回浮点型
    print(time.time())
    
    # 获取当地时间   返回的是结构化时间
    print(time.localtime())
    
    # 时间戳 转结构化
    print(time.localtime(time.time()))
    
    # 结构化 转时间戳
    print(time.mktime(time.localtime()))
    
    # 获取UTC时间 返回的还是结构化时间  比中国时间少8小时
    print(time.gmtime())
    
    # 将获取的时间转成指定的格式 仅支持结构化时间
    print(time.strftime("%Y-%m-%d %H:%M:%S %p", time.localtime()))
    
    # 将格式化字符串的时间转为结构化时间  注意: 格式必须匹配
    print(time.strptime("2018-10-15 15:00:18", "%Y-%m-%d %H:%M:%S"))
    
    # sleep 让当前进程睡眠一段时间 单位是秒
    time.sleep(2)
    print('over')
    
    # 接受时间元组并返回一个可读的形式为"Mon Oct 15 15:09:24 2018"的24个字符的字符串。
    print(time.asctime())
    
    # 获取字符串化的当前时间
    print(time.ctime())
    time

    datetime 模块提供日期和时间运算表示的模块

    datetime常用属性

    import datetime
    
    # 获取当前时间 返回的是格式化字符时间
    print(datetime.datetime.now())
    
    # 单独获取某个时间 年 月
    d = datetime.datetime.now()
    print(d.year)
    print(d.day)
    
    # 手动指定时间
    d2 = datetime.datetime(2018,10,10,10,10,10)
    print(d2)
    
    # 计算两个时间的差  只能减不能加
    print(d - d2)
    
    # 替换某个时间
    print(d.replace(year=2020))
    
    # 表示时间差的属性 timedelta
    print(datetime.timedelta(days=1))
    
    d = datetime.datetime.now()
    t1 = datetime.timedelta(days=1)
    t2 = datetime.timedelta(weeks=1)
    print(t2 - t1)
    # 时间差可以和一个datetime进行加减
    print(d + t2)
    datetime

    random 模块:随机数模块

    # random() 获取0-1之间的随机小数
    #  格式:random.random()
    #  返回值:随机0-1之间的小数
    
    print(random.random())
    
    # 运行
    0.4549137928915099
    random.random()
    # choice() 随机返回序列中的某个值
    #  格式:random.choice(序列)
    #  返回值:序列中的某个值
    
    l = [str(i)+"haha" for i in range(5)]
    print(l)
    rst = random.choice(l)
    print(rst)
    
    # 运行
    ['0haha', '1haha', '2haha', '3haha', '4haha']
    3haha
    random.choice()
    # shuffle() 随机打乱列表
    #  格式:random.shuffle(列表)
    #  返回值:打乱顺序之后的列表
    
    l1 = [i for i in range(5)]
    print(l1)
    
    random.shuffle(l1)
    print(l1)
    
    # 运行
    [0, 1, 2, 3, 4]
    [2, 3, 0, 4, 1]
    random.shuffle()
    # randint(a,b): 返回一个a到b之间的随机整数,包含a和b
    
    print(random.randint(0,100))
    
    # 运行
    46
    random.randint()
    # randrange(a,b): 返回一个a到b之间的随机整数,包含a不包含b
    
    print(random.randrange(0,2))
    
    # 运行
    0
    random.randrange()
    # 随机选指定个数
    print(random.sample([1,2,3],2))
    
    # 运行
    [2, 3]
    random.sample()

    实现随机验证码,整型和全大写字符,可指定长度

    def make_code(i):
        res = ""
        for j in range(i):
    #       随机0到9
            num = str(random.randint(0,9))
            c = chr(random.randint(65,90))
            s = random.choice([num,c])
            res += s
        return res
    print(make_code(4))
    随机验证码
  • 相关阅读:
    Spring Cloud
    如何修改容器时间而不改变宿主机时间?
    消息中间件 RabbitMQ 入门篇
    CentOS7下NFS服务安装及配置固定端口
    查看ssh有没有被黑的IP
    JVM常用命令和性能调优建议
    nfs高可用
    kafka集群部署以及单机部署
    OSGI企业应用开发(十三)OSGI Web应用开发(二)
    OSGI企业应用开发(十二)OSGI Web应用开发(一)
  • 原文地址:https://www.cnblogs.com/qiuxirufeng/p/9791686.html
Copyright © 2020-2023  润新知