• Day13 Python基础之time/datetime/random模块一(十一)


    time模块

    import time

    print(help(time))

    time.time()  #return current time in seconds since the Epoch as a float 时间戳:以秒的形式返回当前时间,从1970年算起

    time.clock()  #return CPU time since process start as a float 只计算CPU执行的时间

    time.sleep()    # delay for a number of seconds given as a float

    time.gmtime()   # convert seconds since Epoch to UTC tuple 结构化时间(UTC英国格林尼治天文台为零时区,北京在东八区,时差为8 个小时)

    time.localtime()   #convert seconds since Epoch to local time tuple(本地时间)结构化时间

     

    time.strftime()  # convert time tuple to string according to format specification 格式化时间

     1  Commonly used format codes:
     2     
     3     %Y  Year with century as a decimal number.
     4     %m  Month as a decimal number [01,12].
     5     %d  Day of the month as a decimal number [01,31].
     6     %H  Hour (24-hour clock) as a decimal number [00,23].
     7     %M  Minute as a decimal number [00,59].
     8     %S  Second as a decimal number [00,61].
     9     %z  Time zone offset from UTC.
    10     %a  Locale's abbreviated weekday name.
    11     %A  Locale's full weekday name.
    12     %b  Locale's abbreviated month name.
    13     %B  Locale's full month name.
    14     %c  Locale's appropriate date and time representation.
    15     %I  Hour (12-hour clock) as a decimal number [01,12].
    16     %p  Locale's equivalent of either AM or PM.
    17 
    18 实例:
    19 import time
    20 print(time.strftime('%Y--%m--%d %H:%M:%S '))
    21 输出结果:
    22 2018--06--13 11:52:04  <class 'str'>
    strftime(format)

    time.strptime((string, format))  #parse string to time tuple according to format specification字符串时间转化成格式化时间

    1 b=time.strptime('2018--06--13 12:02:03','%Y--%m--%d %H:%M:%S')
    2 print(b)
    3 print(b.tm_year)
    4 print(b.tm_min)
    5 输出结果:
    6 time.struct_time(tm_year=2018, tm_mon=6, tm_mday=13, tm_hour=12, tm_min=2, tm_sec=3, tm_wday=2, tm_yday=164, tm_isdst=-1)
    7 2018
    8 2
    strptime(string,format)

     

    time.ctime(second)   #Convert a time in seconds since the Epoch to a string in local time.把秒的时间转换成格式化本地时间

    print(time.ctime(123456))
    输出结果:
    Fri Jan  2 18:17:36 1970
    ctime()

    time.mktime()      #Convert a time tuple in local time to seconds since the Epoch.  #把本地时间转换成秒

    1 print(time.mktime(time.localtime()))
    2 输出结果:
    3 1528863479.0
    mktime()

     datetime模块

    import datetime

    1 1 import  datetime
    2 2 print(datetime.datetime.now())
    3 3 输出结果:
    4 4 2018-06-13 12:21:02.344843
    datetime

     random模块

    import random

    random.random()  #x in the interval [0, 1).

     

    random.randint(1,8)  #int x in inteval[1,8]

    random.randrange(1,8)  #int x in inteval[1,8)

     

    random.choice(sequence)   #Choose a random element from a non-empty sequence.

    random.sample(sequence,count) #Choose count  random elements  from a non-empty sequence.

     1 # 验证码函数
     2 import  random
     3 def v_code():
     4     code=''
     5     for i in range(5):
     6         add_num=random.randrange(0,9)
     7         add_al=chr(random.randrange(65,91))
     8         # if random.randint(1,2)==1:
     9         #     code+=str(add_num)
    10         # else:
    11         #     code+=add_al
    12         code+=random.choice([str(add_num),add_al])
    13     print(code)
    14 
    15 v_code()
    随机产生验证码实例

     注:可以通过chr把数字转换成相应的字母

     

     

     

  • 相关阅读:
    DNN 4.9补丁
    15款经典图表软件推荐 创建最漂亮的图表
    在.NET使用JSON作为数据交换格式
    JQuery 动态删除
    在ASP.NET中使用Highcharts js图表
    一个比较好的工具网站
    DataTable与实体类互相转换
    第一次写JQuery页面
    一个代码查询网址
    NioEventLoop
  • 原文地址:https://www.cnblogs.com/weinapang/p/9168392.html
Copyright © 2020-2023  润新知