zoukankan      html  css  js  c++  java
  • Python基础学习五 内置模块

    time 模块

     1 >>> import time
     2 >>> time.time()
     3 1491064723.808669
     4 >>> # time.time()返回当前时间的时间戳timestamp(定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数)的方法,无参数
     5 >>> time.asctime()
     6 'Sun Apr  2 00:39:32 2017'
     7 >>> # time.asctime()把struct_time对象格式转换为字符串格式为'Sun Apr  2 00:39:32 2017'
     8 >>> time.asctime(time.gmtime())
     9 'Sat Apr  1 16:41:41 2017'
    10 >>> time.asctime(time.localtime())
    11 'Sun Apr  2 00:42:06 2017'
    12 >>> time.ctime()
    13 'Sun Apr  2 00:42:29 2017'
    14 >>> # time.ctime()把时间戳转换为字符串格式'Sun Apr  2 00:42:29 2017',默认为当前时间戳
    15 >>> time.ctime(1491064723.808669)
    16 'Sun Apr  2 00:38:43 2017'
    17 >>> time.altzone  # 返回与utc时间的时间差,以秒计算
    18 -32400
    19 >>> time.localtime()  # 把时间戳转换为struct_time对象格式,默认返回当前时间戳
    20 time.struct_time(tm_year=2017, tm_mon=4, tm_mday=2, tm_hour=0, tm_min=45, tm_sec=26, tm_wday=6, tm_yday=92, tm_isdst=0)
    21 >>> time.localtime(1491064723.808669)
    22 time.struct_time(tm_year=2017, tm_mon=4, tm_mday=2, tm_hour=0, tm_min=38, tm_sec=43, tm_wday=6, tm_yday=92, tm_isdst=0)
    23 >>> 
    24 >>> time.gmtime()   # 将utc时间戳转换成struct_time对象格式,默认返回当前时间的
    25 time.struct_time(tm_year=2017, tm_mon=4, tm_mday=1, tm_hour=16, tm_min=46, tm_sec=32, tm_wday=5, tm_yday=91, tm_isdst=0)
    26 >>> time.gmtime(1491064723.808669)
    27 time.struct_time(tm_year=2017, tm_mon=4, tm_mday=1, tm_hour=16, tm_min=38, tm_sec=43, tm_wday=5, tm_yday=91, tm_isdst=0)
    28 >>> 
    29 >>> 
    30 >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 将本地时间的struct_time格式转成自定义字符串格式 2017-04-01 23:15:47
    31 '2017-04-02 00:47:49'
    32 >>> 
    33 >>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())  # 将utc时间的struct_time格式转成自定义字符串格式 2017-04-01 23:15:47
    34 '2017-04-01 16:48:27'
    35 >>> 
    36 >>> time.strptime('2017-04-02 00:47:49', '%Y-%m-%d %H:%M:%S')   # 将 日期字符串 转成 struct_time时间对象格式,注意转换后的tm_isdst=-1()夏令时状态
    37 time.struct_time(tm_year=2017, tm_mon=4, tm_mday=2, tm_hour=0, tm_min=47, tm_sec=49, tm_wday=6, tm_yday=92, tm_isdst=-1)
    38 >>> 
    39 >>> time.mktime(time.localtime())
    40 1491065416.0
    41 >>> # 将struct_tiame时间对象转成时间戳 结果返回1491061855.0  ,忽略小于秒的时间(忽略小数点后面)
    42 >>> 
    43 >>> time.mktime(time.localtime(1491061855.0011407))
    44 1491061855.0
    45 >>> # 结果返回1491061855.0  ,忽略小于秒的时间(忽略小数点后面)
    46 >>> 
    47 >>> time.mktime(time.gmtime(1491061855.0011407))
    48 1491033055.0
    49 >>> 
    50 >>> # 结果返回1491033055.0  ,忽略小于秒的时间(忽略小数点后面)
    51 >>>

     时间转换关系图

    格式字符及意义

    %a 星期的简写。如 星期三为Web
    %A 星期的全写。如 星期三为Wednesday
    %b 月份的简写。如4月份为Apr
    %B月份的全写。如4月份为April 
    %c:  日期时间的字符串表示。(如: 04/07/10 10:43:39)
    %d:  日在这个月中的天数(是这个月的第几天)
    %f:  微秒(范围[0,999999])
    %H:  小时(24小时制,[0, 23])
    %I:  小时(12小时制,[0, 11])
    %j:  日在年中的天数 [001,366](是当年的第几天)
    %m:  月份([01,12])
    %M:  分钟([00,59])
    %p:  AM或者PM
    %S:  秒(范围为[00,61],为什么不是[00, 59],参考python手册~_~)
    %U:  周在当年的周数当年的第几周),星期天作为周的第一天
    %w:  今天在这周的天数,范围为[0, 6],6表示星期天
    %W:  周在当年的周数(是当年的第几周),星期一作为周的第一天
    %x:  日期字符串(如:04/07/10)
    %X:  时间字符串(如:10:43:39)
    %y:  2个数字表示的年份
    %Y:  4个数字表示的年份
    %z:  与utc时间的间隔 (如果是本地时间,返回空字符串)
    %Z:  时区名称(如果是本地时间,返回空字符串)

    datetime模块,方便时间计算

     1 >>> import datetime
     2 >>> datetime.datetime.now()
     3 datetime.datetime(2017, 4, 7, 16, 52, 3, 199458)
     4 # 返回一组数据(年,月,日,小时,分钟,秒,微秒)
     5 
     6 >>> print(datetime.datetime.now())
     7 2017-04-07 16:52:55.000164
     8 # 打印返回格式(固定)
     9 
    10 >>> datetime.datetime.now()+datetime.timedelta(days=3)
    11 datetime.datetime(2017, 4, 10, 16, 53, 51, 180847)
    12 # 时间加(减),可以是日,秒,微秒,毫秒,分,小时,周
    13 #days=0, seconds=0, microseconds=0,milliseconds=0, minutes=0, hours=0, weeks=0
    14 >>> print(datetime.datetime.now()+datetime.timedelta(weeks=1))
    15 2017-04-17 16:54:08.916243
    16 
    17 >>> datetime.datetime.now().replace(minute=3,hour=2)
    18 datetime.datetime(2017, 4, 7, 2, 3, 11, 163663)
    19 # 时间替换
    20 
    21 >>> datetime.datetime.now()
    22 datetime.datetime(2017, 4, 7, 16, 58, 22, 195439)
    23 
    24 >>> datetime.datetime.now().replace(day=1,month=1)
    25 datetime.datetime(2017, 1, 1, 16, 59, 13, 210556)
    26 >>> 
    27 # 直接替换相应位置数据

    random模块

    import random
    >>> print(random.random())
    0.5364503211492734
    >>> print(random.randint(1,10))
    3
    >>> # 整数1-10(包括10),随机取一个值
    >>> 
    >>> 
    >>> 
    >>> print(random.randrange(1, 10))
    8
    >>> # 整数1-10(不包括10),随机取一个值
     1 import random
     2 
     3 checkcode = ''
     4 for i in range(6):
     5     current = random.randrange(0, 6)
     6     if current != i and current+1 != i:
     7         temp = chr(random.randint(65, 90))
     8         # 65-90是A-Z
     9     elif current+1 == i:
    10         temp = chr(random.randint(97, 122))
    11         # 97-122是a-z
    12     else:
    13         temp = random.randint(0, 9)
    14     checkcode += str(temp)
    15 print(checkcode)
    16 
    17 # 一共6位验证码,
    18 # 第一位有1/6几率是数字,其它都是大写字母
    19 # 第二到第六位,都是有1/6几率是小写字母,1/6几率是数字,其它都是大写字母

    OS模块 

    提供对操作系统进行调用的接口

     1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
     2 os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
     3 os.curdir  返回当前目录: ('.')
     4 os.pardir  获取当前目录的父目录字符串名:('..')
     5 os.makedirs('dirname1/dirname2')    可生成多层递归目录
     6 os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
     7 os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
     8 os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
     9 os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
    10 os.remove()  删除一个文件
    11 os.rename("oldname","newname")  重命名文件/目录
    12 os.stat('path/filename')  获取文件/目录信息
    13 os.sep    输出操作系统特定的路径分隔符,win下为"\",Linux下为"/"
    14 os.linesep    输出当前平台使用的行终止符,win下为"	
    ",Linux下为"
    "
    15 os.pathsep    输出用于分割文件路径的字符串
    16 os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
    17 os.system("bash command")  运行shell命令,直接显示
    18 os.environ  获取系统环境变量
    19 os.path.abspath(path)  返回path规范化的绝对路径
    20 os.path.split(path)  将path分割成目录和文件名二元组返回
    21 os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
    22 os.path.basename(path)  返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即os.path.split(path)的第二个元素
    23 os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
    24 os.path.isabs(path)  如果path是绝对路径,返回True
    25 os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
    26 os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
    27 os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
    28 os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
    29 os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间

    sys模块

    用于提供对解释器相关的操作

    1 sys.argv           命令行参数List,第一个元素是程序本身路径
    2 sys.exit(n)        退出程序,正常退出时exit(0)
    3 sys.version        获取Python解释程序的版本信息
    4 sys.maxint         最大的Int值
    5 sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
    6 sys.platform       返回操作系统平台名称
    7 sys.stdout.write('please:')
    8 val = sys.stdin.readline()[:-1]
    
    

    datetime模块

     1 print(datetime.datetime.today()) #获取当前时间,到秒
     2 print(datetime.datetime.now()) #和today一样
     3 
     4 res = datetime.datetime.today().strftime('%Y-%m-%d') #格式化好的时间
     5 print(res)
     6 
     7 res= datetime.datetime.today()+datetime.timedelta(-3) #3天前的时间
     8 res= datetime.datetime.today()+datetime.timedelta(3) #3天后的时间
     9 print(res)
    10 
    11 print(datetime.date.today()) #去当天的日期,只是日期
  • 相关阅读:
    有点忙啊
    什么是协程
    HDU 1110 Equipment Box (判断一个大矩形里面能不能放小矩形)
    HDU 1155 Bungee Jumping(物理题,动能公式,弹性势能公式,重力势能公式)
    HDU 1210 Eddy's 洗牌问题(找规律,数学)
    HDU1214 圆桌会议(找规律,数学)
    HDU1215 七夕节(模拟 数学)
    HDU 1216 Assistance Required(暴力打表)
    HDU 1220 Cube(数学,找规律)
    HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)
  • 原文地址:https://www.cnblogs.com/louis-w/p/8317206.html
Copyright © 2011-2022 走看看