day22_模块(二)
补充
import sys,os ll = os.path.dirname(os.path.abspath(__file__)) #上一层目录 print(ll) print(os.path.abspath(__file__)) #文件绝对路径 BASE_DIR = sys.path.append(ll) #添加到环境变量中
os
print(os.getcwd()) #当前目录 os.chdir("..") print(os.getcwd()) #到上一层 # os.makedirs('dirname1/dirname2') #递归建立dirname1/dirname2 os.mkdir('wws') #创建一个目录 os.rename('wws','wa') #修改名字 os.removedirs("dirname1/dirname2") #递归删除, 删除不为空的目录 print(os.listdir()) #当前目录下的所有文件,目录 print(os.stat("1.py")) #文件的状态 print(os.path.join('wa','ww')) #路径拼接 print (os.path.split('D:/python/pythontest/练习1/13.py')) #('D:/python/pythontest/练习1', '13.py') print (os.path.exists('1.py')) #判断是否存在 print(os.system("dir")) #运行系统命令
sys
sys.exit(n) sys.path sys.argv
import time for i in range(100): sys.stdout.write("#") time.sleep(0.01) sys.stdout.flush() ret = '' for i in range(1,101): ret +='#' time.sleep(0.1) print(ret,int((i/100)*100),'%')
json
import json """ json把数据都写成str类型 """ dac = {"name":"yang"} with open("json_file",'w') as f: date = json.dumps(dac) f.write(date) with open("json_file")as f: data = json.loads(f.read()) print(data)
f_name = open("json_file_1",'w') data = json.dumps(dac) f_name.write(data) f_name.close() f_name = open("json_file_1") ff = json.loads(f_name.read()) print(ff) f_name.close()
pickle
import pickle """ pickle 都是bytes 所以打开都需要加上b """ dac = {"name":"yang"} with open("pickle_file","wb") as f: pp = pickle.dumps(dac) f.write(pp) with open("pickle_file","rb")as f: pp = pickle.loads(f.read()) print(pp)
dac = {"name":"yang"} f = open("pickle_file1","wb") f.write(pickle.dumps(dac)) f.close() f = open("pickle_file1","rb") pp = pickle.loads(f.read()) print(pp) f.close()
re
""" . 通配符 除\n 匹配一位 ^ 匹配字符串开头 $ 匹配字符串结尾 * 匹配前面字符 0~无穷次 贪婪匹配 + 匹配前面字符 1~无穷次 贪婪匹配 ? 匹配前面字符 0~1次 贪婪匹配 {2,3} 匹配前面 2到3次 贪婪匹配 {1,} 匹配前面 1到无穷次 贪婪匹配 {0,} 匹配前面 0到无穷次 贪婪匹配 """
print(re.findall('w.',"w2ekhd2")) #['w2'] print(re.findall('^e.',"e2wkhd2")) #['e2'] #匹配以2开头 print(re.findall('\w+2$',"e2wkhd2")) #['e2wkhd2'] #匹配任何字母数字以2结尾的 print(re.findall('e+',"eeee2wkhd2")) #['eeee'] 匹配前面的字符1到无穷次 print(re.findall('2w*',"eeee2wkhd2")) #['2w', '2'] 匹配前面一个字符的0到无穷次 print(re.findall("y{2,3}g","jsjkyyygskll")) #['yyyg'] 匹配前面
[]
""" x[yz] xy 或xz 特殊功能的字符集[] [-] 范围 [^] [^a-z] 匹配不在a到z """
print(re.findall("x[yz]","xz")) #['xz'] y或者z print(re.findall("[a-f]","dkljklf")) #只取a到f范围 ['d', 'f'] print(re.findall("[^a-f]","dkljklf")) #不在a到f范围内 ['k', 'l', 'j', 'k', 'l']
转移符 \
""" 转移符 \ 1. \后面跟元字符去除特殊功能 \. 2. \普通字符变成,特殊功能 \w 没有意思的, 变成有意义 \d 匹配任意十进制数 [0-9] \D 匹配任意非数字字符 [^0-9] \s 匹配任何空字符 [\t\n\r\f\v] \S 匹配任何非空字符 [^\t\n\r\f\v] \w 匹配任何字母数字字符 [a-zA-Z0-9_] \W 匹配任何非字母数字字符 [^a-zA-Z0-9_] \b 匹配一个特殊字符边界 空格 # & """
print(re.findall("\d+","dsdss274033augustyangskkljw")) #匹配0到9 +是贪婪匹配 ['274033'] print(re.findall("\D+","dsdss274033augustyangsk?#kljw")) #匹配非数字的 ['dsdss', 'augustyangsk?#kljw'] print(re.findall("\s+","dsdss\t\n2740\n33aungsk?#kljw")) #匹配任何空字符 ['\t\n', '\n'] print(re.findall("\S+","dsdss\t\n2740\n33aungsk?#kljw")) #匹配任何非空字符 ['dsdss', '2740', '33aungsk?#kljw'] print(re.findall("\w+","dsdss\t\n33aungsk?_#kljw")) #匹配任何字母数字字符 ['dsdss', '33aungsk', '_', 'kljw'] print(re.findall("\W+","dsdss\t\n33aungsk?_#kljw")) #匹配任何非字母数字字符 ['\t\n', '?', '#'] print(re.findall(r'I\b','I am LIST')) #['I']
posted on 2018-05-22 17:38 augustyang 阅读(231) 评论(0) 编辑 收藏 举报