python进阶学习——第五天

一模块

  1.1 time 和 datetime

 1 import time
 2 print(time.time())  # 以秒的形式返回当前时间,从1970年1月1日开始计算  1455613795.636903,一天为86400秒
 3 print(time.altzone)  # 时间差,与标准的格林威治时间相差8小时,以秒返回 -28800.0
 4 print(time.localtime())  # 以元组形式显示当地时间
 5 print(time.asctime(time.localtime()))  # 将元组形式的时间格式转换成可读性的时间格式 Tue Feb 16 16:52:51 2016
 6 print(time.clock())  # 返回CPU执行时间,或第一次调用clock函数的时间 0.047894
 7 print(time.ctime())  # 返回可读性的时间 Tue Feb 16 16:56:59 2016 等效于asctime(localtime(seconds))
 8 print(time.gmtime())  # 以元组形式显示格林威治时间
 9 print(time.localtime(0))
10 print(time.mktime(time.gmtime(2)))  # -28798.0
11 print(time.mktime(time.gmtime(0)))  # 当秒为0,不会返回0,而是以秒形式返回当前时区的timezone或altzone的值 -28800.0
12 print(time.monotonic())
13 print(time.perf_counter())
14 print(time.process_time())  # 返回内核到用户空闲的CPU这段时间0.045212
15 print(time.sleep(0.01))  # 睡眠0.01秒
16 print(time.strftime("%p", time.localtime()))  # 格式时间
17 #     %Y  Year with century as a decimal number.  # 年
18 #     %m  Month as a decimal number [01,12].  # 月
19 #     %d  Day of the month as a decimal number [01,31].  # 日
20 #     %H  Hour (24-hour clock) as a decimal number [00,23].  #小时
21 #     %M  Minute as a decimal number [00,59].  # 分钟
22 #     %S  Second as a decimal number [00,61].  # 秒
23 #     %z  Time zone offset from UTC.  # 当前时区
24 #     %a  Locale's abbreviated weekday name.  # 星期几,例如Tue
25 #     %A  Locale's full weekday name.  # 全拼星期几, Tuesday
26 #     %b  Locale's abbreviated month name. # 几月份,例如 Feb
27 #     %B  Locale's full month name.  # 全拼月, February
28 #     %c  Locale's appropriate date and time representation.  # 显示这种格式时间 Tue Feb 16 17:32:19 2016
29 #     %I  Hour (12-hour clock) as a decimal number [01,12].  # 以12小时进制显示当前时间
30 #     %p  Locale's equivalent of either AM or PM.  # 显示上午还是下午
31 
32 print(time.strptime("2016", "%Y"))  # 以元组形式返回一个结构化时间,
33 print(time.tzset())  # 初始化本地时区或重新初始化

   1.2 datetime

1 import datetime
2 print(datetime.datetime.now())  # 显示当前时间
3 print(datetime.timedelta(days=5))
4 print(datetime.datetime.now() - datetime.timedelta(days=5))  # 显示5天前的时间
5 print(datetime.date.today())  # 显示当前日期
6 print(datetime.date.ctime(datetime.date.today()))  # 显示可读性时间
7 print(datetime.date.strftime(datetime.date.today(), "%Y"))  # 格式化时间
 1 import time
 2 import datetime
 3 print(datetime.date.fromtimestamp(time.time()))
 4 print(datetime.date.fromtimestamp(time.time() - 86400))  # 减去一天的秒数
 5 print(datetime.datetime.now().date())
 6 print(datetime.datetime.now().time())
 7 print(datetime.datetime.now().today())
 8 print(datetime.datetime.now().year)
 9 print(datetime.datetime.now().weekday())  # 打印星期几,从开始,0-6,0为周日
10 print(datetime.datetime.now().utctimetuple())  # 打印UTC struct时间
11 print(datetime.datetime.now().utcoffset())
12 print(datetime.datetime.now().tzname())
13 print(datetime.datetime.now().toordinal())
14 print(datetime.datetime.now().timetz())
15 print(datetime.datetime.now().timetuple())  # 打印struct时间
16 print(datetime.datetime.now().timestamp())  # 显示时间戳
17 print(datetime.datetime.now().month)  # 显示月
18 print(datetime.datetime.now().replace(2016, 9, 12))  # 替换指定时间打印
19 print(datetime.datetime.now() - datetime.datetime.now().replace(2016, 2, 11))  # 计算时间差
20 print(datetime.datetime.now() + datetime.timedelta(seconds=10))  # 加10秒,显示10秒后的时间

   1.3 os模块

 1 print(os)
 2 print(os.getcwd())  # 获取当前目录
 3 print(os.chdir('/'))  # 切换到/目录
 4 print(os.getcwd())
 5 print(os.curdir)  # 当前目录
 6 print(os.pardir)  # 上一级目录
 7 print(os.makedirs("test/test2", mode=777, exist_ok=True))  # 创建多层目录
 8 print(os.removedirs("test"))  # 移除目录
 9 print(os.mkdir("test", mode=777))  # 创建单层目录
10 print(os.rmdir('test'))  # 删除空目录
11 print(os.listdir(os.curdir))  # 显示当前目录文件
12 print(os.remove("test"))  # 删除test文件
13 print(os.rename("old", "newname"))  # 重命名文件,如果当前存在重名文件,则报错
14 print(os.stat("re_ext.py"))
15 print(os.sep)  # 打印系统路径分隔符
16 print(list(os.linesep))  # 打印系统换行符
17 print(os.pathsep)  # 打印path变量分隔符
18 print(os.name)  # 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
19 print(os.system("command"))  # 执行系统命令
20 print(os.environ)  # 打印环境变量
21 print(os.path.abspath(os.curdir))  # 打印当前绝对路径
22 print(os.path.split(os.curdir))  # 讲目录分隔为二元组返回
23 print(os.path.dirname(os.pardir))  # 打印目录
24 print(os.path.basename(os.curdir))  # 打印当前文件
25 print(os.path.exists("test"))  # 打印当前是否存在test文件
26 print(os.path.isabs(os.path.abspath(os.curdir)))  # 判断是否绝对路径
27 
28 print(os.path.isfile("test"))  # 判断test是不是一个文件
29 print(os.path.isdir("test"))  # 判断test是不是一个目录
30 print(os.path.join("/test", "test2"))  # 将多个路径组合成一个新路径,忽略绝对路径之前的参数
31 print(os.path.getatime("re_ext.py"))  # 打印文件的最后存取时间,时间撮形式返回
32 print(os.path.getmtime("re_ext.py"))  # 打印文件最后修改时间
33 print(os.path.getsize("re_ext.py"))  # 打印文件大小

     1.4 sys 模块

 1 import sys
 2 print(sys.argv)  # 列表形式返回命令行参数,第一个元素是文件本身
 3 print(sys.modules)  # 打印系统所有模块,字典形式返回
 4 # print(sys.exit(2))  # 打印程序返回代码
 5 print(sys.version)  # 打印python版本
 6 print(sys.maxsize)  # 打印最大值
 7 print(sys.path)  # 返回当前PYTHONPATH环境变量
 8 print(sys.platform)  # 返回操作系统平台名称
 9 print(sys.stdout.write("inpu9t"))  # 返回标准输出长度
10 # print(sys.stdin.readlin()[:-1])  # 标准输入,并分片取开头到倒数第一个(不包含)的内容
11 print(sys.modules.keys())  # 打印所有模块名
12 print(sys.modules.values())  # 打印所有模块位置
13 print(sys.exc_info())  # 获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息
14 print(sys.hexversion)  # 获取十六进制的版本值
15 print(sys.api_version)  # 获取Cpython API版本
16 print(sys.version_info)  # 打印版本详细信息
17 print(sys.displayhook(2))  # 如果value非空,这个函数会把他输出到sys.stdout,并且将他保存进__builtin__._.指在python的交互式解释器里,’_’ 代表上次你输入得到的结果,hook是钩子的意思,将上次的结果钩过来
18 print(sys.getdefaultencoding())  # 获取当前默认编码格式
19 print(sys.getfilesystemencoding())  # 获取文件系统编码
20 print(sys.builtin_module_names)  # 打印内置模块名
21 print(sys.executable)  # 打印python解释权程序路径
22 print(sys.getprofile())
23 print(sys.getallocatedblocks())
24 # print(sys.copyright)  # 打印python版权信息
25 print(sys.byteorder)  # 本地字节规则的指示器,big-endian平台的值是’big’,little-endian平台的值是’little’
26 # print(sys.exc_value)
27 print(sys.stdin)  # 标准输入
28 print(sys.stdout)  # 标准输出
29 print(sys.stderr)  # 标准错误输出
30 print(sys.maxunicode)  #   打印最大unicode值

 

二 正则补充

  2.1 match search

 1 import re
 2 print(re.match('www', "www.baidu.com"))  # 打印匹配结果
 3 print(re.match('www', "www.baidu.com").group())  # 打印匹配到的结果
 4 print(re.match('www', "www.baidu.com").end())  # 打印匹配到的结束位置,不包含该索引字符
 5 print(re.match('www', "www.baidu.comw").endpos)  # 打印最后一个字符串位置
 6 print(re.match('www', "www.baidu.com").groupdict())
 7 print(re.match('www', "www.baidu.com").groups())
 8 print(re.match('www', "www.baidu.com").lastindex)
 9 print(re.match('www', "www.wwwbaidu.com").pos)  # 打印匹配索引
10 print(re.match('www', "www.baidu.com").re)  # 打印编译(解析公式)
11 print(re.match('ww', "www.baidu.com").span())  # 打印匹配分片的位置
12 print(re.match('www', "www.baidu.com").start())  # 打印匹配开始索引
13 print(re.match('www', "www.baidu.com").string)  # 打印被匹配字符串
14 
15 print(re.search("www", "www.www.com"))
16 print(re.search("www", "qq.www.com"))
17 print(re.search("www", "www.www.com").string)
18 print(re.search("www", "www.www.com").start())
19 print(re.search("www", "qq.www.com").start())
20 print(re.search("www", "qq.www.com").span())  # 返回匹配到的索引位置
21 print(re.search("www", "www.www.com").span())
22 print(re.search("www", "qq.www.com").group())
23 print(re.search("www", "qq.www.com").groups())
24 print(re.search("www", "qq.www.com"))
25 
26 # search 与match,
27 # search 从头匹配,只要找到则停止匹配,如果有多个被匹配,只返回最先匹配到的位置字符
28 # match  从头匹配,开头位置匹配不到就不继续匹配

    正则分组匹配模式 ()

1 a = "123abc456"
2 print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group())  # 模式()分组匹配
3 print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0))  # 显示第0组,也就是所有分组
4 print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1))  # 显示第一组分组
5 print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2))
6 print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups())  # 返回元组形式的分组匹配

 

1 print(re.findall("\d+", "gghf23812uygc4x36123y9hsoy93y2932"))  # 返回一个列表
2 
3 print(re.split(",", "445dfds77fg45cxxc3fsdbuu"))  # 以逗号分隔
4 print(re.split("\d+", "445dfds77fg45cxxc3fsdbuu", maxsplit=2))  # 分三列

   匹配中文

1 re.findall("[\u4e00-\u9fa5]+", string)

 

1 contactInfo = 'Oldboy School, Beijing Changping Shahe: 010-8343245'
2 match = re.search('(?P<last>\w+\s+\w+),(?P<first>\s+\w+\s+\w+\s+\w+): (?P<phone>\S+)', contactInfo)
3 print(match.group("last"))  # 打印自定义标签内容
4 print(match.group('first'))
5 print(match.group("phone"))

 

posted @ 2016-02-16 17:40  曾春云  阅读(413)  评论(0)    收藏  举报