import os for k, v in os.environ.items(): print("%s=%s" % (k, v)); import sys; # sys.modules得到模块的引用 print(‘\n‘.join(sys.modules.keys())); # 引入文件系统 import os; print(os.path.join(‘c:\\damp\\mp3‘,‘young.mp3‘)); # c:\damp\mp3\young.mp3 print(os.path.expanduser(‘~‘)); # C:\Users\Administrator print(os.path.join(os.path.expanduser(‘~‘), ‘Python‘)); # C:\Users\Administrator\Python (shortname, extens) = os.path.splitext(‘young.mp3‘); print(shortname); # young print(extens); # .mp3 ###########新的页面################## # coding=gbk import os; print(os.listdir(‘./images‘)); import glob; print(glob.glob(‘e:\\demo\\python\\images\\*.jpg‘)); ‘‘‘ [‘e:\\demo\\python\\images\\01.jpg‘, ‘e:\\demo\\python\\images\\03.jpg‘, ‘e:\\demo\\python\\images\\04.jpg‘, ‘e:\\demo\\python\\images\\05.jpg‘, ‘e:\\demo\\python\\images\\06.jpg‘] ‘‘‘ print(glob.glob(‘e:\\demo\\python\\images\\*‘)); ‘‘‘ [‘e:\\demo\\python\\images\\01.jpg‘, ‘e:\\demo\\python\\images\\02.png‘, ‘e:\\demo\\python\\images\\03.jpg‘, ‘e:\\demo\\python\\images\\04.jpg‘, ‘e:\\demo\\python\\images\\05.jpg‘, ‘e:\\demo\\python\\images\\06.jpg‘] ‘‘‘
正则表达式:
Python特有的修饰符:
修辞符 描述
re.I 执行不区分大小写的匹配。
re.L 根据当前的语言环境解释词组。这种解释影响字母组(w和W),以及单词边界的行为(和B)
re.M 使$匹配一行(串的不只是端部)的尾部,使^匹配的行(串不只是开始)的开始
re.S 使一个句号(点)匹配任何字符,包括换行符
re.U 根据Unicode字符集解释的字母。这个标志会影响w, W, , B的行为。
re.X 许可证“cuter”正则表达式语法。它忽略空格(除了一组[]或当用一个反斜杠转义内),并把转义#作为注
释标记
代码区:
import re; line = ‘Cats are smarter than dogs.‘; searchObj = re.match(r‘(.*) are (.*?) .*‘, line, re.M|re.I); if searchObj: print(searchObj.group()); print(searchObj.group(1)); print(searchObj.group(2)); else: print(‘match none‘); searchObj2 = re.search(r‘dogs‘, line, re.M|re.I); print(‘searchObj2: ‘, searchObj2.group()); # searchObj2: dogs ######################## import re; phone = ‘2222-0000-9999 # This is my phone num.‘; phone_num = re.sub(r‘#.*$‘, ‘‘, phone); print(phone_num); # 2222-0000-9999
时间: 2024-11-29 09:14:02