常見的場景:一個模塊就是一個包含了python定義和聲明的文件,文件名就是模塊名字加上.py的後綴。 python
1 使用python編寫的代碼(.py文件)mysql
3 包好一組模塊的包redis
4 使用C編寫並連接到python解釋器的內置模塊算法
1.importsql
2.from ... improt ...shell
模塊導入查找順序:內存->內置->sys.path (sys.path是執行文件的路徑,sys.path是python的搜索模塊的路徑集,是一個list)json
import sys import os print(sys.path) res=os.path.dirname(os.path.dirname(__file__)) sys.path.insert(0,res) #添加文件的搜索路徑 print(sys.path) '''['E:\\study\\code\\day5\\模塊\\模塊的搜索路徑', 'E:\\study\\code', 'D:\\python36\\python36.zip', 'D:\\python36\\DLLs', 'D:\\python36\\lib', 'D:\\python36', 'D:\\python36\\lib\\site-packages'] ['E:/study/code/day5/模塊', 'E:\\study\\code\\day5\\模塊\\模塊的搜索路徑', 'E:\\study\\code', 'D:\\python36\\python36.zip', 'D:\\python36\\DLLs', 'D:\\python36\\lib', 'D:\\python36', 'D:\\python36\\lib\\site-packages'] '''
sys.modules 和 限制from import導入變量的方法
#spam.py print('from the spam.py') __all__=['money','x'] #對from spam import * 有用,能夠導入__all__中的變量 # _money=1000 #對from spam import * 有用,_開頭的變量不能夠被導入 money=0 x=1 def read1(): print('spam->read1->money',money) def read2(): print('spam->read2 calling read') read1() def change(): global money money=10 #run.py import sys print('spam' in sys.modules) # sys.modules存放的是已經加載到內的模塊,sys.module是一個字典,內部包含模塊名與模塊對象的映射,該字典決定了導入模塊時是否須要從新導入 import spam #模塊只在第一次導入時纔會執行,以後的導入都是直接引用內存已經存在的結果 print('spam' in sys.modules) import spam
'''
False
from the spam.py
Truewindows
'''
爲了提升加載模塊的速度,強調強調強調:提升的是加載速度而絕非運行速度。python解釋器會在__pycache__目錄中下緩存每一個模塊編譯後的版本,格式爲:module.version.pyc。一般會包含python的版本號。例如,在CPython3.3版本下,spam.py模塊會被緩存成__pycache__/spam.cpython-33.pyc。這種命名規範保證了編譯後的結果多版本共存。 緩存
Python檢查源文件的修改時間與編譯的版本進行對比,若是過時就須要從新編譯。這是徹底自動的過程。而且編譯的模塊是平臺獨立的,因此相同的庫能夠在不一樣的架構的系統之間共享,即pyc使一種跨平臺的字節碼,相似於JAVA火.NET,是由python虛擬機來執行的,可是pyc的內容跟python的版本相關,不一樣的版本編譯後的pyc文件不一樣,2.5編譯的pyc文件不能到3.5上執行,而且pyc文件是能夠反編譯的,於是它的出現僅僅是用來提高模塊的加載速度的。bash
1.文件當作腳本運行時__name__等於__main__
2.文件當作模塊被加載運行時__name__等於模塊名
#m1.py def func1(): print('from m1') print('file name is:%s' % __name__) #__name__ if __name__ == '__main__': #當作腳本使用 func1() ''' file name is:__main__ from m1 ''' #run.py import m1 m1.func1() '''file name is:m1 from m1'''
1 """ 2 logging配置 3 """ 4 5 import os 6 import logging.config 7 8 # 定義三種日誌輸出格式 開始 9 10 standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ 11 '[%(levelname)s][%(message)s]' #其中name爲getlogger指定的名字 12 13 simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' 14 15 id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s' 16 17 # 定義日誌輸出格式 結束 18 19 logfile_dir = os.path.dirname(os.path.abspath(__file__)) # log文件的目錄 20 21 logfile_name = 'all2.log' # log文件名 22 23 # 若是不存在定義的日誌目錄就建立一個 24 if not os.path.isdir(logfile_dir): 25 os.mkdir(logfile_dir) 26 27 # log文件的全路徑 28 logfile_path = os.path.join(logfile_dir, logfile_name) 29 30 # log配置字典 31 LOGGING_DIC = { 32 'version': 1, 33 'disable_existing_loggers': False, 34 'formatters': { 35 'standard': { 36 'format': standard_format 37 }, 38 'simple': { 39 'format': simple_format 40 }, 41 'id_simple': { 42 'format': id_simple_format 43 }, 44 }, 45 'filters': {}, 46 'handlers': { 47 #打印到終端的日誌 48 'console': { 49 'level': 'DEBUG', 50 'class': 'logging.StreamHandler', # 打印到屏幕 51 'formatter': 'simple' 52 }, 53 #打印到文件的日誌,收集info及以上的日誌 54 'default': { 55 'level': 'DEBUG', 56 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 57 'formatter': 'standard', 58 'filename': logfile_path, # 日誌文件 59 'maxBytes': 1024*1024*5, # 日誌大小 5M 60 'backupCount': 5, 61 'encoding': 'utf-8', # 日誌文件的編碼,不再用擔憂中文log亂碼了 62 }, 63 'boss': { 64 'level': 'DEBUG', 65 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 66 'formatter': 'standard', 67 'filename': 'boss.log', # 日誌文件 68 'maxBytes': 1024 * 1024 * 5, # 日誌大小 5M 69 'backupCount': 5, 70 'encoding': 'utf-8', # 日誌文件的編碼,不再用擔憂中文log亂碼了 71 }, 72 }, 73 'loggers': { 74 #logger1=logging.getLogger(__name__)拿到的logger配置 75 '': { 76 'handlers': ['default', 'console'], # 這裏把上面定義的兩個handler都加上,即log數據既寫入文件又打印到屏幕 77 'level': 'DEBUG', 78 'propagate': True, # 向上(更高level的logger)傳遞 79 }, 80 #logger1=logging.getLogger('collect')拿到的logger配置 81 'collect': { 82 'handlers': ['boss',], # 這裏把上面定義的兩個handler都加上,即log數據既寫入文件又打印到屏幕 83 'level': 'DEBUG', 84 'propagate': True, # 向上(更高level的logger)傳遞 85 }, 86 }, 87 } 88 89 90 def load_my_logging_cfg(): 91 logging.config.dictConfig(LOGGING_DIC) # 導入上面定義的logging配置 92 # logger = logging.getLogger(__name__) # 生成一個log實例 93 # logger.info('It works!') # 記錄該文件的運行狀態 94 95 if __name__ == '__main__': 96 load_my_logging_cfg()
1 # =================================匹配模式================================= 2 #一對一的匹配 3 # 'hello'.replace(old,new) 4 # 'hello'.find('pattern') 5 6 #正則匹配 7 import re 8 #\w與\W 9 print(re.findall('\w','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] 10 print(re.findall('\W','hello egon 123')) #[' ', ' '] 11 12 #\s與\S 13 print(re.findall('\s','hello egon 123')) #[' ', ' ', ' ', ' '] 14 print(re.findall('\S','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3'] 15 16 #\n \t都是空,均可以被\s匹配 17 print(re.findall('\s','hello \n egon \t 123')) #[' ', '\n', ' ', ' ', '\t', ' '] 18 19 #\n與\t 20 print(re.findall(r'\n','hello egon \n123')) #['\n'] 21 print(re.findall(r'\t','hello egon\t123')) #['\n'] 22 23 #\d與\D 24 print(re.findall('\d','hello egon 123')) #['1', '2', '3'] 25 print(re.findall('\D','hello egon 123')) #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' '] 26 27 #\A與\Z 28 print(re.findall('\Ahe','hello egon 123')) #['he'],\A==>^ 29 print(re.findall('123\Z','hello egon 123')) #['he'],\Z==>$ 30 31 #^與$ 32 print(re.findall('^h','hello egon 123')) #['h'] 33 print(re.findall('3$','hello egon 123')) #['3'] 34 35 # 重複匹配:| . | * | ? | .* | .*? | + | {n,m} | 36 #. 37 print(re.findall('a.b','a1b')) #['a1b'] 38 print(re.findall('a.b','a1b a*b a b aaab')) #['a1b', 'a*b', 'a b', 'aab'] 39 print(re.findall('a.b','a\nb')) #[] 40 print(re.findall('a.b','a\nb',re.S)) #['a\nb'] 41 print(re.findall('a.b','a\nb',re.DOTALL)) #['a\nb']同上一條意思同樣 42 43 #* 44 print(re.findall('ab*','bbbbbbb')) #[] 45 print(re.findall('ab*','a')) #['a'] 46 print(re.findall('ab*','abbbb')) #['abbbb'] 47 48 #? 49 print(re.findall('ab?','a')) #['a'] 50 print(re.findall('ab?','abbb')) #['ab'] 51 #匹配全部包含小數在內的數字 52 print(re.findall('\d+\.?\d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3'] 53 54 #.*默認爲貪婪匹配 55 print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b'] 56 57 #.*?爲非貪婪匹配:推薦使用 58 print(re.findall('a.*?b','a1b22222222b')) #['a1b'] 59 60 #+ 61 print(re.findall('ab+','a')) #[] 62 print(re.findall('ab+','abbb')) #['abbb'] 63 64 #{n,m} 65 print(re.findall('ab{2}','abbb')) #['abb'] 66 print(re.findall('ab{2,4}','abbb')) #['abb'] 67 print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+' 68 print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*' 69 70 #[] 71 print(re.findall('a[1*-]b','a1b a*b a-b')) #[]內的都爲普通字符了,且若是-沒有被轉意的話,應該放到[]的開頭或結尾 72 print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]內的^表明的意思是取反,因此結果爲['a=b'] 73 print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]內的^表明的意思是取反,因此結果爲['a=b'] 74 print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]內的^表明的意思是取反,因此結果爲['a=b'] 75 print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]內的^表明的意思是取反,因此結果爲['a=b'] 76 77 #\# print(re.findall('a\\c','a\c')) #對於正則來講a\\c確實能夠匹配到a\c,可是在python解釋器讀取a\\c時,會發生轉義,而後交給re去執行,因此拋出異常 78 print(re.findall(r'a\\c','a\c')) #r表明告訴解釋器使用rawstring,即原生字符串,把咱們正則內的全部符號都當普通字符處理,不要轉義 79 print(re.findall('a\\\\c','a\c')) #同上面的意思同樣,和上面的結果同樣都是['a\\c'] 80 81 #():分組 82 print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab'] 83 print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab 84 print(re.findall('(?:ab)+123','ababab123')) #findall的結果不是匹配的所有內容,而是組內的內容,?:可讓結果爲匹配的所有內容 85 86 #| 87 print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
re經常使用方法
1 # ===========================re模塊提供的方法介紹=========================== 2 import re 3 #findall 4 s = "zyltest zyl1 mynamezyl lingling" 5 print(re.findall('zyl',s)) #['zyl', 'zyl', 'zyl'],返回全部知足匹配條件的結果,放到列表中;沒有匹配時,返回空列表 6 7 #2 search 8 print(re.search('zyl',s)) #<_sre.SRE_Match object; span=(1, 4), match='zyl'> 9 print(re.search('zyl',s).group()) #zyl 只找到第一個匹配的而後返回一個包含匹配信息的對象,該對象能夠經過調用group()方法獲得匹配的字符串。若是字符串沒有匹配,則返回None 10 print(re.search('ling',s).group()) #ling 11 12 #3 matche 13 print(re.match('zyl',s)) #<_sre.SRE_Match object; span=(0, 3), match='zyl'> 14 print(re.match('ling',s)) #None 同search,不一樣點是match是從字符串開始處進行匹配,徹底能夠用search+^代替match 15 16 #4 split 17 print(re.split('zyl',s)) #['', 'test ', '1 myname', ' lingling'] 按照'zyl'分割,輸出列表 18 print(re.split('[ab]','abcd')) #['', '', 'cd'],先按'a'分割獲得''和'bcd',再對''和'bcd'分別按'b'分割 19 20 #5 sub 21 print(re.sub('zyl','zhaoyanling',s)) #zhaoyanlingtest zhaoyanling1 mynamezhaoyanling lingling,不指定N,默認替換全部 22 print(re.sub('zyl','zhaoyanling',s,1)) #zhaoyanlingtest zyl1 mynamezyl lingling 替換n指定的個數 23 24 print('===>',re.sub('^(\w+)(.*?\s)(\w+)(.*?\s)(\w+)(.*?)$',r'\5\2\3\4\1','alex make love')) #===> love make alex 25 26 print('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),結果帶有總共替換的個數 27 28 29 #6 compile 能夠複用須要查找對象 30 obj = re.compile('zyl') 31 print(obj,type(obj)) #re.compile('zyl') <class '_sre.SRE_Pattern'> 32 print(obj.search(s).group()) 33 print(obj.findall(s)) 34 35 obj1=re.compile('\d{2}') 36 print(obj1.search('abc123eeee').group()) #12 37 print(obj1.findall('abc123eeee')) #['12'],重用了obj
補充一
1 import re 2 print(re.findall("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>")) #['h1'] 3 print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>").group()) #<h1>hello</h1> 4 print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>").groupdict()) #<h1>hello</h1> 5 6 print(re.search(r"<(\w+)>\w+</(\w+)>","<h1>hello</h1>").group()) 7 print(re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>").group()) 8 9 '''['h1'] 10 <h1>hello</h1> 11 {'tag_name': 'h1'} 12 <h1>hello</h1> 13 <h1>hello</h1>''' 14 15 # | 先匹配左邊的,若是左邊知足就不會再匹配右邊 16 print(re.findall(r'-?\d+\.?\d*',"1-12*(60+(-40.35/5)-(-4*3))")) #找出全部數字['1', '-12', '60', '-40.35', '5', '-4', '3'] 17 #使用|,先匹配的先生效,|左邊是匹配小數,而findall最終結果是查看分組,全部即便匹配成功小數也不會存入結果 18 #而不是小數時,就去匹配(-?\d+),匹配到的天然就是,非小數的數,在此處即整數 19 print(re.findall(r"-?\d+\.\d*|(-?\d+)","1-2*(60+(-40.35/5)-(-4*3))")) #找出全部整數['1', '-2', '60', '', '5', '-4', '3'] 20 print(re.findall(r"-?\d+","1-2*(60+(-40.35/5)-(-4*3))")) #['1', '-2', '60', '-40', '35', '5', '-4', '3'] 21 22 #search與findall 23 #爲什麼一樣的表達式search與findall卻有不一樣結果: 24 print(re.search('\(([\+\-\*\/]*\d+\.?\d*)+\)',"1-12*(60+(-40.35/5)-(-4*3))").group()) #(-40.35/5) 25 print(re.findall('\(([\+\-\*\/]*\d+\.?\d*)+\)',"1-12*(60+(-40.35/5)-(-4*3))")) #['/5', '*3'] 26 27 #看這個例子:(\d)+至關於(\d)(\d)(\d)(\d)...,是一系列分組 28 print(re.search('(\d)+','123').group()) #group的做用是將全部組拼接到一塊兒顯示出來 29 print(re.findall('(\d)+','123')) #findall結果是組內的結果,且是最後一個組的結果
補充二
1 #_*_coding:utf-8_*_ 2 #在線調試工具:tool.oschina.net/regex/# 3 import re 4 5 s=''' 6 http://www.baidu.com 7 egon@oldboyedu.com 8 你好 9 010-3141 10 ''' 11 12 #最常規匹配 13 # content='Hello 123 456 World_This is a Regex Demo' 14 # res=re.match('Hello\s\d\d\d\s\d{3}\s\w{10}.*Demo',content) 15 # print(res) 16 # print(res.group()) 17 # print(res.span()) 18 19 #泛匹配 20 # content='Hello 123 456 World_This is a Regex Demo' 21 # res=re.match('^Hello.*Demo',content) 22 # print(res.group()) 23 24 25 #匹配目標,得到指定數據 26 27 # content='Hello 123 456 World_This is a Regex Demo' 28 # res=re.match('^Hello\s(\d+)\s(\d+)\s.*Demo',content) 29 # print(res.group()) #取全部匹配的內容 30 # print(res.group(1)) #取匹配的第一個括號內的內容 31 # print(res.group(2)) #去陪陪的第二個括號內的內容 32 33 34 35 #貪婪匹配:.*表明匹配儘量多的字符 36 # import re 37 # content='Hello 123 456 World_This is a Regex Demo' 38 # 39 # res=re.match('^He.*(\d+).*Demo$',content) 40 # print(res.group(1)) #只打印6,由於.*會盡量多的匹配,而後後面跟至少一個數字 41 42 43 #非貪婪匹配:?匹配儘量少的字符 44 # import re 45 # content='Hello 123 456 World_This is a Regex Demo' 46 # 47 # res=re.match('^He.*?(\d+).*Demo$',content) 48 # print(res.group(1)) #只打印6,由於.*會盡量多的匹配,而後後面跟至少一個數字 49 50 51 #匹配模式:.不能匹配換行符 52 content='''Hello 123456 World_This 53 is a Regex Demo 54 ''' 55 # res=re.match('He.*?(\d+).*?Demo$',content) 56 # print(res) #輸出None 57 58 # res=re.match('He.*?(\d+).*?Demo$',content,re.S) #re.S讓.能夠匹配換行符 59 # print(res) 60 # print(res.group(1)) 61 62 63 #轉義:\ 64 65 # content='price is $5.00' 66 # res=re.match('price is $5.00',content) 67 # print(res) 68 # 69 # res=re.match('price is \$5\.00',content) 70 # print(res) 71 72 73 #總結:儘可能精簡,詳細的以下 74 # 儘可能使用泛匹配模式.* 75 # 儘可能使用非貪婪模式:.*? 76 # 使用括號獲得匹配目標:用group(n)去取得結果 77 # 有換行符就用re.S:修改模式 78 79 80 81 #re.search:會掃描整個字符串,不會從頭開始,找到第一個匹配的結果就會返回 82 83 # import re 84 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 85 # 86 # res=re.match('Hello.*?(\d+).*?Demo',content) 87 # print(res) #輸出結果爲None 88 89 # 90 # import re 91 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 92 # 93 # res=re.search('Hello.*?(\d+).*?Demo',content) # 94 # print(res.group(1)) #輸出結果爲 95 96 97 98 #re.search:只要一個結果,匹配演練, 99 import re 100 content=''' 101 <tbody> 102 <tr id="4766303201494371851675" class="even "><td><div class="hd"><span class="num">1</span><div class="rk "><span class="u-icn u-icn-75"></span></div></div></td><td class="rank"><div class="f-cb"><div class="tt"><a href="/song?id=476630320"><img class="rpic" src="http://p1.music.126.net/Wl7T1LBRhZFg0O26nnR2iQ==/19217264230385030.jpg?param=50y50&quality=100"></a><span data-res-id="476630320" " 103 # res=re.search('<a\shref=.*?<b\stitle="(.*?)".*?b>',content) 104 # print(res.group(1)) 105 106 107 #re.findall:找到符合條件的全部結果 108 # res=re.findall('<a\shref=.*?<b\stitle="(.*?)".*?b>',content) 109 # for i in res: 110 # print(i) 111 112 113 114 #re.sub:字符串替換 115 import re 116 content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 117 118 # content=re.sub('\d+','',content) 119 # print(content) 120 121 122 #用\1取得第一個括號的內容 123 #用法:將123與456換位置 124 # import re 125 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 126 # 127 # # content=re.sub('(Extra.*?)(\d+)(\s)(\d+)(.*?strings)',r'\1\4\3\2\5',content) 128 # content=re.sub('(\d+)(\s)(\d+)',r'\3\2\1',content) 129 # print(content) 130 131 132 133 134 # import re 135 # content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings' 136 # 137 # res=re.search('Extra.*?(\d+).*strings',content) 138 # print(res.group(1)) 139 140 141 # import requests,re 142 # respone=requests.get('https://book.douban.com/').text 143 144 # print(respone) 145 # print('======'*1000) 146 # print('======'*1000) 147 # print('======'*1000) 148 # print('======'*1000) 149 # res=re.findall('<li.*?cover.*?href="(.*?)".*?title="(.*?)">.*?more-meta.*?author">(.*?)</span.*?year">(.*?)</span.*?publisher">(.*?)</span.*?</li>',respone,re.S) 150 # # res=re.findall('<li.*?cover.*?href="(.*?)".*?more-meta.*?author">(.*?)</span.*?year">(.*?)</span.*?publisher">(.*?)</span>.*?</li>',respone,re.S) 151 # 152 # 153 # for i in res: 154 # print('%s %s %s %s' %(i[0].strip(),i[1].strip(),i[2].strip(),i[3].strip()))
os是與操做系統交互的接口
1 import os 2 print(os.getcwd()) #獲取當前工做目錄,即當前python腳本工做的目錄路徑 3 print(os.chdir('/Users/yanlingzhao/Documents/python')) #改變當前腳本工做目錄;至關於shell下cd 4 print(os.getcwd()) 5 '''/Users/yanlingzhao/Documents/python/code/code/6 6 None 7 /Users/yanlingzhao/Documents/python''' 8 9 # os.pardir 獲取當前目錄的父目錄字符串名:('..') 10 # os.makedirs('dirname1/dirname2') 可生成多層遞歸目錄 11 # os.removedirs('dirname1') 若目錄爲空,則刪除,並遞歸到上一級目錄,如若也爲空,則刪除,依此類推 12 # os.mkdir('dirname') 生成單級目錄;至關於shell中mkdir dirname 13 # os.rmdir('dirname') 刪除單級空目錄,若目錄不爲空則沒法刪除,報錯;至關於shell中rmdir dirname 14 # os.listdir('dirname') 列出指定目錄下的全部文件和子目錄,包括隱藏文件,並以列表方式打印 15 # os.remove() 刪除一個文件 16 # os.rename("oldname","newname") 重命名文件/目錄 17 # os.stat('path/filename') 獲取文件/目錄信息 18 # os.sep 輸出操做系統特定的路徑分隔符,win下爲"\\",Linux下爲"/" 19 # os.linesep 輸出當前平臺使用的行終止符,win下爲"\t\n",Linux下爲"\n" 20 # os.pathsep 輸出用於分割文件路徑的字符串 win下爲;,Linux下爲: 21 # os.name 輸出字符串指示當前使用平臺。win->'nt'; Linux->'posix' 22 # os.system("bash command") 運行shell命令,直接顯示 23 # os.environ 獲取系統環境變量 24 25 print(os.path.abspath(__file__)) 26 print(os.path.split(__file__)) 27 print(os.path.dirname(__file__)) 28 print(os.path.basename(__file__)) 29 print(os.path.exists(__file__)) 30 print(os.path.isabs(__file__)) 31 print(os.path.isfile(__file__)) 32 print(os.path.isdir) 33 print(os.path.join(os.path.dirname(__file__),os.path.basename(__file__))) 34 print(os.path.getatime(__file__)) 35 print(os.path.getctime(__file__)) 36 print(os.path.getsize(__file__)) 37 '''/Users/yanlingzhao/Documents/python/code/code/6/osmodule.py 38 ('/Users/yanlingzhao/Documents/python/code/code/6', 'osmodule.py') 39 /Users/yanlingzhao/Documents/python/code/code/6 40 osmodule.py 41 True 42 True 43 True 44 <function isdir at 0x1005d9bf8> 45 /Users/yanlingzhao/Documents/python/code/code/6/osmodule.py 46 1502596131.0 47 1502596131.0 48 3169''' 49 # os.path.abspath(path) 返回path規範化的絕對路徑 50 # os.path.split(path) 將path分割成目錄和文件名二元組返回 51 # os.path.dirname(path) 返回path的目錄。其實就是os.path.split(path)的第一個元素 52 # os.path.basename(path) 返回path最後的文件名。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素 53 # os.path.exists(path) 若是path存在,返回True;若是path不存在,返回False 54 # os.path.isabs(path) 若是path是絕對路徑,返回True 55 # os.path.isfile(path) 若是path是一個存在的文件,返回True。不然返回False 56 # os.path.isdir(path) 若是path是一個存在的目錄,則返回True。不然返回False 57 # os.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第一個絕對路徑以前的參數將被忽略 58 # os.path.getatime(path) 返回path所指向的文件或者目錄的最後存取時間 59 # os.path.getmtime(path) 返回path所指向的文件或者目錄的最後修改時間 60 # os.path.getsize(path) 返回path的大小
補充
在Linux和Mac平臺上,該函數會原樣返回path,在windows平臺上會將路徑中全部字符轉換爲小寫,並將全部斜槓轉換爲飯斜槓。 >>> os.path.normcase('c:/windows\\system32\\') 'c:\\windows\\system32\\' 規範化路徑,如..和/ >>> os.path.normpath('c://windows\\System32\\../Temp/') 'c:\\windows\\Temp' >>> a='/Users/jieli/test1/\\\a1/\\\\aa.py/../..' >>> print(os.path.normpath(a)) /Users/jieli/test1 在Linux和Mac平臺上,該函數會原樣返回path,在windows平臺上會將路徑中全部字符轉換爲小寫,並將全部斜槓轉換爲飯斜槓。 >>> os.path.normcase('c:/windows\\system32\\') 'c:\\windows\\system32\\' 規範化路徑,如..和/ >>> os.path.normpath('c://windows\\System32\\../Temp/') 'c:\\windows\\Temp' >>> a='/Users/jieli/test1/\\\a1/\\\\aa.py/../..' >>> print(os.path.normpath(a)) /Users/jieli/test1
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]
1 import sys,time 2 3 print(sys.argv) #運行文件時,輸入參數以列表格式輸出;第一個是文件名 4 '''python sysmodule.py 1 5 ['sysmodule.py', '1'] 6 ''' 7 8 print(sys.modules) #查看運行文件已經加載模塊 9 print(sys.path) #模塊索索路徑,輸出的是列表格式;能夠經過append或insert添加所需搜索路徑 10 print(sys.platform) 11 print(sys.version) 12 13 #在 Python 中打印對象調用 print obj 時候,事實上是調用了 sys.stdout.write(obj+'\n') 14 #如下兩行是等價的 15 print('test') 16 sys.stdout.write('test'+'\n') 17 18 for i in range(1,100): 19 sys.stdout.write('\r%s' %('#'*i)) #'\r%s' \r能夠是下次還從頭打印 20 sys.stdout.flush() #實時刷新數據到終端 21 time.sleep(0.5) 22 23 print(sys.stdin) 24 in1 = input('test:') 25 26 print('test:') 27 in2 = sys.stdin.readline()[:-1] 28 29 print(in1) 30 print(in2) 31 '''<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'> 32 test:123 33 test: 34 456 35 123 36 456'''
模擬進度條
1 import sys 2 import time 3 def progress(percent,width=50): 4 if percent >= 100: 5 print('\r[%s] %d%%' %('#'*width,100)) #print的end默認爲回車 6 return 7 8 show_str=('[%%-%ds]' %width) %(int(width * percent / 100) * "#") #字符串拼接的嵌套使用 9 print("\r%s %d%%" %(show_str, percent),end='',file=sys.stdout,flush=True) 10 11 #=========應用========== 12 data_size=100 13 recv_size=0 14 while recv_size < data_size: 15 time.sleep(1) #模擬數據的傳輸延遲 16 recv_size+=10 #每次收10 17 18 recv_per=int(100*(recv_size/data_size)) #接收的比例 19 progress(recv_per,width=30) #進度條的寬度30
什麼是序列化:把對象(變量)從內存中變成可存儲或傳輸的過程成爲序列化。
序列化的目的:持久存儲狀態;跨平臺數據交換
json.dumps. json.loads. json.dump. json.load
1 import json 2 3 dic = {'a': 1, 'b': 2, 'c': 3} 4 5 print(type(dic)) 6 with open('a.txt','w') as f: 7 f.write(str(dic)) 8 9 ''' 10 <class 'dict'> 11 a.txt: 12 {'a': 1, 'b': 2, 'c': 3} 13 ''' 14 15 json_dic = json.dumps(dic) 16 print(type(json_dic)) 17 with open('b.txt','w') as f: 18 f.write(json_dic) 19 ''' 20 <class 'str'> 21 b.txt: 22 {"a": 1, "b": 2, "c": 3} 23 ''' 24 25 with open('a.txt','r') as f: 26 res = f.read() 27 print(res,type(res)) 28 ''' 29 {'a': 1, 'b': 2, 'c': 3} 30 <class 'str'> 31 ''' 32 33 with open('b.txt','r') as f: 34 res = f.read() 35 print(res,type(res)) 36 json_res = json.loads(res) 37 print(json_res,type(json_res)) 38 ''' 39 {"a": 1, "b": 2, "c": 3} <class 'str'> 40 {'a': 1, 'b': 2, 'c': 3} <class 'dict'> 41 ''' 42 43 json.dump(dic,open('c.json','w')) #dump 直接把json序列化的數據寫入文件 44 with open('c.json','r') as f: 45 print(f.read()) 46 json_pare = json.load(open('c.json','r')) #load直接把json反序列化的數據讀出 47 print(json_pare,type(json_pare)) 48 ''' 49 {"a": 1, "b": 2, "c": 3} 50 {'a': 1, 'b': 2, 'c': 3} <class 'dict'> 51 '''
1 import json 2 #dct="{'1':111}"#json 不認單引號 3 #dct=str({"1":111})#報錯,由於生成的數據仍是單引號:{'one': 1} 4 5 dct='{"1":"111"}' 6 print(json.loads(dct)) 7 8 #conclusion: 9 # 不管數據是怎樣建立的,只要知足json格式,就能夠json.loads出來,不必定非要dumps的數據才能loads 10 11 注意點
json和python內置數據類型對應關係:
json | python |
{} | dict |
[] | list |
"string" | str |
123.33 | int/float |
true/false | True/False |
null | None |
pickle模塊用法
import pickle dic_pik = pickle.dumps(dic) print(type(dic_pik)) #<class 'bytes'> #注意是w是寫入str,wb是寫入bytes,dic_pik是'bytes' with open('a.plk','wb') as f: #必須以b的方式打開 f.write(dic_pik) with open('a.plk','rb') as f: res = pickle.loads(f.read()) print(res,type(res)) #{'a': 1, 'b': 2, 'c': 3} <class 'dict'> pickle.dump(dic,open('b.pkl','wb')) red = pickle.load(open('b.pkl','rb')) print(res,type(res)) #{'a': 1, 'b': 2, 'c': 3} <class 'dict'>
class Foo: pass obj1=Foo() obj2=Foo() pickle.dump(obj1,open('class.pkl','wb')) print(obj1) import pickle res = pickle.load(open('class.pkl','rb')) print(res) ''' <__main__.Foo object at 0x102260518> <__main__.Foo object at 0x102260ac8> '''
在Python中,一般有這幾種方式來表示時間:
1 import time 2 3 print(time.time()) 4 print(time.strftime('%Y-%m-5d %X')) 5 print(time.localtime()) 6 print(time.gmtime()) 7 '''1501988136.581536 8 2017-08-5d 10:55:36 9 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=6, tm_hour=10, tm_min=55, tm_sec=36, tm_wday=6, tm_yday=218, tm_isdst=0) 10 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=6, tm_hour=2, tm_min=55, tm_sec=36, tm_wday=6, tm_yday=218, tm_isdst=0) 11 '''
1 # 將一個時間戳轉換爲當前時區的struct_time。secs參數未提供,則以當前時間爲準。 2 print(time.localtime().tm_hour) #加參數獲取相應字段值 3 print(time.localtime(1473525444)) 4 '''11 5 time.struct_time(tm_year=2016, tm_mon=9, tm_mday=11, tm_hour=0, tm_min=37, tm_sec=24, tm_wday=6, tm_yday=255, tm_isdst=0)''' 6 7 # gmtime([secs]) 和localtime()方法相似,gmtime()方法是將一個時間戳轉換爲UTC時區(0時區)的struct_time。 8 9 # mktime(t) : 將一個struct_time轉化爲時間戳。 10 print(time.mktime(time.localtime())) 11 '''1501989047.0''' 12 13 # strftime(format[, t]) : 把一個表明時間的元組或者struct_time(如由time.localtime()和 14 # time.gmtime()返回)轉化爲格式化的時間字符串。若是t未指定,將傳入time.localtime()。若是元組中任何一個 15 # 元素越界,ValueError的錯誤將會被拋出。 16 print(time.strftime('%Y-%m-%d %X',time.localtime())) 17 '''2017-08-06 11:14:35 18 ''' 19 20 # time.strptime(string[, format]) 21 # 把一個格式化時間字符串轉化爲struct_time。實際上它和strftime()是逆操做。 22 print(time.strptime('2017-08-06 11:14:35', '%Y-%m-%d %X')) 23 '''time.struct_time(tm_year=2017, tm_mon=8, tm_mday=6, tm_hour=11, tm_min=14, tm_sec=35, tm_wday=6, tm_yday=218, tm_isdst=-1) 24 ''' 25 #在這個函數中,format默認爲:"%a %b %d %H:%M:%S %Y"
1 # asctime([t]) : 把一個表示時間的元組或者struct_time表示爲這種形式:'Sun Jun 20 23:21:05 1993'。 2 # 若是沒有參數,將會將time.localtime()做爲參數傳入。 3 print(time.asctime()) 4 print(time.asctime(time.gmtime())) 5 '''Sun Aug 6 11:26:43 2017 6 Sun Aug 6 03:26:43 2017''' 7 8 # ctime([secs]) : 把一個時間戳(按秒計算的浮點數)轉化爲time.asctime()的形式。若是參數未給或者爲 9 # None的時候,將會默認time.time()爲參數。它的做用至關於time.asctime(time.localtime(secs))。 10 print(time.ctime()) 11 print(time.ctime(time.time())) 12 '''Sun Aug 6 11:26:43 2017 13 Sun Aug 6 11:26:43 2017'''
time.sleep(0.6) # 線程推遲指定的時間運行,單位爲秒。
datetime用法
import datetime print(datetime.datetime.now()) #獲取當前時間 print(datetime.date.fromtimestamp(time.time())) #當前時間轉化成日期 '''2017-08-06 11:37:39.447878 2017-08-06''' print(datetime.datetime.now()+datetime.timedelta(3)) #當前時間加3天 print(datetime.datetime.now()+datetime.timedelta(-3)) '''2017-08-09 11:39:11.772055 2017-08-03 11:39:11.772063''' print(datetime.datetime.now()+datetime.timedelta(hours=3)) #當前時間加3小時 print(datetime.datetime.now()+datetime.timedelta(hours=-3)) '''2017-08-06 14:41:34.166602 2017-08-06 08:41:34.166614''' c_time = datetime.datetime.now() print(c_time) print(c_time.replace(minute=3,hour=2)) #時間替換 '''2017-08-06 11:42:11.178857 2017-08-06 02:03:11.178857'''
1 import random 2 3 print(random.choice([0,1,7])) #隨機選擇 4 ''' 5 def choice(self, seq): 6 """Choose a random element from a non-empty sequence.""" 7 try: 8 i = self._randbelow(len(seq)) 9 except ValueError: 10 raise IndexError('Cannot choose from an empty sequence') from None 11 return seq[i] 12 ''' 13 14 print(random.randrange(1,10)) #1<=x<10的隨機整數 15 16 print(random.randint(1,10)) #1<=x<=10的隨機整數 17 ''' 18 def randint(self, a, b): 19 """Return random integer in range [a, b], including both end points. 20 """ 21 22 return self.randrange(a, b+1) 23 ''' 24 print(random.random()) #0-1的隨機小數 25 26 print(random.uniform(10,16)) #10-16之間的隨機小數 27 28 print(random.sample([1,2,3,4,5],3)) #列表或元祖中隨機組合3個,3必須小於等於seq的長度 29 30 item=[1,2,3,6,5,43] 31 print(random.shuffle(item)) #打亂原來列表的順序 32 print(item) 33 '''None 34 [6, 5, 3, 2, 43, 1]'''
生成隨機驗證碼
import random def make_code(n): res = '' for i in range(n): s1 = str(random.randint(0, 9)) s2 = chr(random.randint(65, 90)) s = random.choice([s1,s2]) res = res + s return res print(make_code(4)) '''R384''' ''' def chr(*args, **kwargs): # real signature unknown """ Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff. """ pass '''
8.xml模塊
xml實現不一樣語言或程序以前數據交換的格式
xml查找元素的三種方式;查看標籤、屬性和內容的方法:
1 import xml.etree.ElementTree as ET 2 3 tree=ET.parse('a') 4 root=tree.getroot() 5 6 for child in root: 7 print('tags>>>:',child.tag) 8 for i in child: 9 print('tag>>:{} attrid>>:{} text>>:{}'.format(i.tag,i.attrib,i.text)) 10 '''tags>>>: country 11 tag>>:rank attrid>>:{'updated': 'yes'} text>>:2 12 tag>>:year attrid>>:{} text>>:2008 13 tag>>:gdppc attrid>>:{} text>>:141100 14 tag>>:neighbor attrid>>:{'name': 'Austria', 'direction': 'E'} text>>:None 15 tag>>:neighbor attrid>>:{'name': 'Switzerland', 'direction': 'W'} text>>:None 16 tags>>>: country.......''' 17 18 #查找element元素的三種方式 19 years=root.iter('year') #掃描整個xml文檔樹,找到全部 20 for i in years: 21 print(i,i.text) 22 '''<Element 'year' at 0x1022987c8> 2008 23 <Element 'year' at 0x1022b4098> 2011 24 <Element 'year' at 0x1022b4228> 2011''' 25 26 res1 = root.find('year') 27 print(res1) 28 res2=root.find('country') #誰來調,就從誰下一層開始找,只找一個 29 print(res2) 30 '''None 31 <Element 'country' at 0x1005b49a8>''' 32 33 res3=root.findall('country') #誰來調,就從誰下一層開始找,只找一個 34 print(res3) 35 '''[<Element 'country' at 0x1005b49a8>, <Element 'country' at 0x1022aaf98>, <Element 'country' at 0x1022b4188>] 36 '''
xml的增刪改操做:
1 #修改 2 years = root.iter('year') #掃描整個xml文檔述,找到全部years的標籤 3 for year in years: 4 year.text = str(int(year.text)+1) #修改標籤內容 5 year.set('updated','yes') 6 year.set('version','1.0') #增長標籤屬性 7 tree.write('a') #寫入文件,剛纔只是在內存作的修改 8 9 #刪除 10 for country in root.iter('country'): 11 rank = country.find('rank') 12 if int(rank.text) > 10: 13 country.remove(rank) 14 tree.write('a') 15 16 17 #增長節點 18 #在country內添加(append)節點year2 19 import xml.etree.ElementTree as ET 20 tree = ET.parse("a") 21 root=tree.getroot() 22 for country in root.findall('country'): 23 for year in country.findall('year'): 24 if int(year.text) > 2000: 25 year2=ET.Element('year2') 26 year2.text='新年' 27 year2.attrib={'update':'yes'} 28 country.append(year2) #往country節點下添加子節點 29 30 tree.write('a.xml.swap')
9.configparser
解析處理configparser文件
1 import configparser 2 3 config = configparser.ConfigParser() 4 config.read('p4p') 5 6 #取配置 7 print(config.sections()) 8 '''['application', 'ui-p4p-user', 'qa-p4p-user', 'qa-agent-user', 'qa-p4p-redis', 'qa-engine-redis', 'qa-p4p-antiattack', 'qa-omp-mysql', 'qa-p4p-mysql'] 9 以列表的形式返回標題 10 ''' 11 print(config.options(config.sections()[3])) 12 '''['mail', 'password'] 13 查看某個標題下的配置項 14 ''' 15 res = config.get('qa-p4p-redis','redis_host') 16 '''10.165.124.28 <class 'str'> 17 查看某個標題下的某個配置項的值 18 ''' 19 res1 = config.getint('qa-p4p-redis','redis_port') 20 print(res1,type(res1)) 21 '''8801 <class 'int'> 22 #查看某個標題下的某個配置項的值''' 23 24 25 res2=config.getboolean('qa-p4p-redis','redis_database')#查看某個標題下的某個配置項的值 26 print(res2,type(res2)) 27 '''False <class 'bool'>''' 28 29 # res1=config.getfloat('egon','salary')#查看某個標題下的某個配置項的值 30 # print(type(res1)) 31 32 #修改 33 config.remove_section('qa-p4p-mysql') 34 config.remove_option('qa-omp-mysql','mysql_charset') 35 36 config.add_section('qa-p4p-zyl') 37 config.set('qa-p4p-zyl','name','zyl') 38 39 config.write(open('p4p','w')) #寫入文件
#獲取標題下的全部鍵值對 res4 = config.items('qa-omp-mysql') print(res4) '''[('mysql_host', '10.165.124.46'), ('mysql_port', '3306'), ('mysql_user', 'omptest'), ('mysql_password', 'omptest!@#$'), ('mysql_database', 'omp')] ''' #檢查 has_sec=config.has_section('qa-omp-mysql') print(has_sec) #打印True
10.hashlib
hash:一種算法 ,3.x裏代替了md5模塊和sha模塊,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
三個特色:
1.內容相同則hash運算結果相同,內容稍微改變則hash值則變
2.不可逆推
3.相同算法:不管校驗多長的數據,獲得的哈希值長度固定。
1 import hashlib 2 3 m=hashlib.md5()# m=hashlib.sha256() 4 5 m.update('hello'.encode('utf8')) 6 print(m.hexdigest()) #5d41402abc4b2a76b9719d911017c592 7 8 m.update('alvin'.encode('utf8')) 9 10 print(m.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af 11 12 m2=hashlib.md5() 13 m2.update('helloalvin'.encode('utf8')) 14 print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af 15 16 ''' 17 注意:把一段很長的數據update屢次,與一次update這段長數據,獲得的結果同樣 18 可是update屢次爲校驗大文件提供了可能。 19 ''' 20 21 #以上加密算法雖然依然很是厲害,但時候存在缺陷,即:經過撞庫能夠反解。因此,有必要對加密算法中添加自定義key再來作加密。 22 import hashlib 23 24 # ######## 256 ######## 25 26 hash = hashlib.sha256('898oaFs09f'.encode('utf8')) 27 hash.update('alvin'.encode('utf8')) 28 print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7 29 30 31 import hashlib 32 33 # ######## 256 ######## 34 35 hash = hashlib.sha256('898oaFs09f'.encode('utf8')) 36 hash.update('alvin'.encode('utf8')) 37 print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7 38 39 #python 還有一個 hmac 模塊,它內部對咱們建立 key 和 內容 進行進一步的處理而後再加密 40 import hmac 41 h = hmac.new('alvin'.encode('utf8')) 42 h.update('hello'.encode('utf8')) 43 print (h.hexdigest())#320df9832eab4c038b6c1d7ed73a5940 44 45 #要想保證hmac最終結果一致,必須保證: 46 #1:hmac.new括號內指定的初始key同樣 47 #2:不管update多少次,校驗的內容累加到一塊兒是同樣的內容 48 49 import hmac 50 51 h1=hmac.new(b'egon') 52 h1.update(b'hello') 53 h1.update(b'world') 54 print(h1.hexdigest()) 55 56 h2=hmac.new(b'egon') 57 h2.update(b'helloworld') 58 print(h2.hexdigest()) 59 60 h3=hmac.new(b'egonhelloworld') 61 print(h3.hexdigest()) 62 63 ''' 64 f1bf38d054691688f89dcd34ac3c27f2 65 f1bf38d054691688f89dcd34ac3c27f2 66 bcca84edd9eeb86f30539922b28f3981 67 '''
11.subprocess
生成子進程
1 import subprocess 2 3 ''' 4 sh-3.2# ls /Users/egon/Desktop |grep txt$ 5 mysql.txt 6 tt.txt 7 事物.txt 8 ''' 9 10 res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE) 11 res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout, 12 stdout=subprocess.PIPE) 13 14 print(res.stdout.read().decode('utf-8')) 15 16 17 #等同於上面,可是上面的優點在於,一個數據流能夠和另一個數據流交互,能夠經過爬蟲獲得結果真後交給grep 18 res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE) 19 print(res1.stdout.read().decode('utf-8')) 20 21 22 #windows下: 23 # dir | findstr 'test*' 24 # dir | findstr 'txt$' 25 import subprocess 26 res1=subprocess.Popen(r'dir C:\Users\Administrator\PycharmProjects\test\函數備課',shell=True,stdout=subprocess.PIPE) 27 res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout, 28 stdout=subprocess.PIPE) 29 30 print(res.stdout.read().decode('gbk')) #subprocess使用當前系統默認編碼,獲得結果爲bytes類型,在windows下須要用gbk解碼