將本來的字典、列表等內容轉換成一個字符串的過程就叫作序列化node
序列化的目的python
python可序列化的數據類型正則表達式
Python | JSON |
---|---|
dict | object |
list,tuple | array |
str | string |
int,float | number |
True | true |
False | false |
None | null |
Json模塊提供了四個功能:dumps、dump、loads、load算法
import json dic = {'k1':'v1','k2':'v2','k3':'v3'} str_dic = json.dumps(dic) # 序列化:將一個字典轉換成一個字符串 print(type(str_dic),str_dic) dic2 = json.loads(str_dic) print(type(dic2),dic2) # 反序列化:將一個字符串格式的字典轉換成一個字典 list_dic = [1,['a','b','c'],3,{'k1':'v1','k2':'v2'}] str_dic = json.dumps(list_dic) print(type(str_dic),str_dic) list_dic2 = json.loads(str_dic) print(type(list_dic2),list_dic2)
Skipkeys | 1,默認值是False,若是dict的keys內的數據不是python的基本類型,2,設置爲False時,就會報TypeError的錯誤。此時設置成True,則會跳過這類key,3,當它爲True的時候,全部非ASCII碼字符顯示爲\uXXXX序列,只需在dump時將ensure_ascii設置爲False便可,此時存入json的中文便可正常顯示。 |
---|---|
indent | 是一個非負的整型,若是是0就是頂格分行顯示,若是爲空就是一行最緊湊顯示,不然會換行且按照indent的數值顯示前面的空白分行顯示,這樣打印出來的json數據也叫pretty-printed json |
ensure_ascii | 當它爲True的時候,全部非ASCII碼字符顯示爲\uXXXX序列,只需在dump時將ensure_ascii設置爲False便可,此時存入json的中文便可正常顯示。 |
separators | 分隔符,其實是(item_separator, dict_separator)的一個元組,默認的就是(‘,’,’:’);這表示dictionary內keys之間用「,」隔開,而KEY和value之間用「:」隔開。 |
sort_keys | 將數據根據keys的值進行排序 |
import json data = {'name':'陳鬆','sex':'female','age':88} json_dic2 = json.dumps(data,sort_keys=True,indent=2,separators=(',',':'),ensure_ascii=False) print(json_dic2)
json | 用於字符串 和 python數據類型間進行轉換 |
---|---|
pickle | 用於python特有的類型 和 python的數據類型間進行轉換 |
pickle模塊提供了四個功能:dumps、dump(序列化,存)、loads(反序列化,讀)、load
不只能夠序列化字典,列表...能夠把python中任意的數據類型序列化sql
import pickle dic = {'k1':'v1','k2':'v2','k3':'v3'} str_dic = pickle.dumps(dic) print(str_dic) dic2 = pickle.loads(str_dic) print(dic2) import time struct_time = time.localtime(1000000000) print(struct_time) f = open('pickle_file','wb') pickle.dump(struct_time,f) f.close() f = open('pickle_file','rb') struct_time2 = pickle.load(f) print(struct_time2.tm_year)
shelve也是python提供給咱們的序列化工具,比pickle用起來更簡單一些。
shelve只提供給咱們一個open方法,是用key來訪問的,使用起來和字典相似。shell
import shelve f = shelve.open('shelve_file') f['key'] = {'int':10,'str':'hello','float':0.123} f.close() f1 = shelve.open('shelve_file') ret = f1['key'] f1.close() print(ret)
這個模塊有個限制,它不支持多個應用同一時間往同一個DB進行寫操做。因此當咱們知道咱們的應用若是隻進行讀操做,咱們可讓shelve經過只讀方式打開DB數據庫
import shelve f1 = shelve.open('shelve_file',flag='r') ret = f1['key'] f1.close() print(ret)
因爲shelve在默認狀況下是不會記錄待持久化對象的任何修改的,因此咱們在shelve.open()時候須要修改默認參數,不然對象的修改不會保存。json
import shelve f1 = shelve.open('shelve_file') print(f1['key']) f1['key']['k1'] = 'v1' f1.close() f2 = shelve.open('shelve_file',writeback=True) print(f2['key']) f2['key']['k1'] = 'hello' f2.close()
Python的hashlib提供了常見的摘要算法,如MD5,SHA1等等。windows
什麼是摘要算法呢?摘要算法又稱哈希算法、散列算法。它經過一個函數,把任意長度的數據轉換爲一個長度固定的數據串(一般用16進制的字符串表示)。bash
摘要算法就是經過摘要函數f()對任意長度的數據data計算出固定長度的摘要digest,目的是爲了發現原始數據是否被人篡改過。
摘要算法之因此能指出數據是否被篡改過,就是由於摘要函數是一個單向函數,計算f(data)很容易,但經過digest反推data卻很是困難。並且,對原始數據作一個bit的修改,都會致使計算出的摘要徹底不一樣。
import hashlib md5 = hashlib.md5() md5.update('how to use md5 in python hashlib?'.encode('utf-8')) print(md5.hexdigest())
若是數據量很大,能夠分塊屢次調用update(),最後計算的結果是同樣的
import hashlib md5 = hashlib.md5() md5.update('how to use md5 '.encode('utf-8')) md5.update('in python hashlib?'.encode('utf-8')) print(md5.hexdigest())
MD5是最多見的摘要算法,速度很快,生成結果是固定的128 bit字節,一般用一個32位的16進制字符串表示。另外一種常見的摘要算法是SHA1,調用SHA1和調用MD5徹底相似
import hashlib sha1 = hashlib.sha1() sha1.update('how to use md5 '.encode('utf-8')) sha1.update('in python hashlib?'.encode('utf-8')) print(sha1.hexdigest())
任何容許用戶登陸的網站都會存儲用戶登陸的用戶名和口令。如何存儲用戶名和口令呢?方法是存到數據庫表中
name | password --------+---------- michael | 123456 bob | abc999 alice | alice2008
若是使用md5來將保護密碼那麼就是這樣
username | password ---------+--------------------------------- michael | e10adc3949ba59abbe56e057f20f883e bob | 878ef96e86145580c38c87f0410ad153 alice | 99b1c2188db85afee403b1536010c2c9
有不少md5撞庫工具,能夠輕鬆的將簡單密碼給碰撞出來
因此,要確保存儲的用戶口令不是那些已經被計算出來的經常使用口令的MD5,這一方法經過對原始口令加一個複雜字符串來實現,俗稱「加鹽」
通過Salt處理的MD5口令,只要Salt不被黑客知道,即便用戶輸入簡單口令,也很難經過MD5反推明文口令。
可是若是有兩個用戶都使用了相同的簡單口令好比123456,在數據庫中,將存儲兩條相同的MD5值,這說明這兩個用戶的口令是同樣的。
若是假定用戶沒法修改登陸名,就能夠經過把登陸名做爲Salt的一部分來計算MD5,從而實現相同口令的用戶也存儲不一樣的MD5。
顯示進度條
import time for i in range(0,101,2): time.sleep(0.1) char_num = i//2 per_str = '\r%s%% : %s\n' % (i, '*' * char_num) \ if i == 100 else '\r%s%% : %s' % (i,'*'*char_num) print(per_str,end='', flush=True)
該模塊適用於配置文件的格式與windows ini文件相似,能夠包含一個或多個節(section),每一個節能夠有多個參數(鍵=值)。
常見的文檔格式
[DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no
使用python生成一個這樣的文件
import configparser conf = configparser.ConfigParser() conf['DEFAULT'] = {'ServerAliveInterval':'45', 'Compression':'yes', 'CompressionLevel':'9', 'ForwardX11':'yes' } conf['bitbucket.org'] = {'User':'hg'} conf['topsecret.server.com'] = {'Port':'50022', 'ForwardX11':'no' } with open('config','w') as config: conf.write(config)
查找
import configparser conf = configparser.ConfigParser() conf['DEFAULT'] = {'ServerAliveInterval':'45', 'Compression':'yes', 'CompressionLevel':'9', 'ForwardX11':'yes' } conf['bitbucket.org'] = {'User':'hg'} conf['topsecret.server.com'] = {'Port':'50022', 'ForwardX11':'no' } print('bitbucket.org' in conf) print('bitbucket.com' in conf) print(conf['bitbucket.org']['user']) print(conf['DEFAULT']['Compression']) for key in conf['bitbucket.org']: print(key) # DEFAULT的鍵也會出現 print(conf.options('bitbucket.org')) # 同for循環,找到'bitbucket.org'下全部鍵 print(conf.items('bitbucket.org')) # 找到'bitbucket.org'下全部鍵值對 print(conf.get('bitbucket.org','compression'))
增刪改操做
import configparser conf = configparser.ConfigParser() conf.read('config') conf.add_section('yuan') # 添加鍵 conf.remove_section('bitbucket.org') # 刪除鍵 conf.remove_option('topsecret.server.com','forwardx11') # 移除條目 conf.set('topsecret.server.com','k1','11111') # 在對應鍵下加上條目 conf.set('yuan','k2','22222') conf.write(open('config.new','w')) # 寫入文件
import logging logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
默認狀況下Python的logging模塊將日誌打印到了標準輸出中,且只顯示了大於等於WARNING級別的日誌,這說明默認的日誌級別設置爲WARNING(日誌級別等級CRITICAL > ERROR > WARNING > INFO > DEBUG),默認的日誌格式爲日誌級別:Logger名稱:用戶輸出消息。
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='test.log', filemode='w') logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
參數解釋
import logging logger = logging.getLogger() # 建立一個handler,用於寫入日誌文件 fh = logging.FileHandler('test.log',encoding='utf-8') # 再建立一個handler,用於輸出到控制檯 ch = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setLevel(logging.DEBUG) fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) #logger對象能夠添加多個fh和ch對象 logger.addHandler(ch) logger.debug('logger debug message') logger.info('logger info message') logger.warning('logger warning message') logger.error('logger error message') logger.critical('logger critical message')
logging庫提供了多個組件:Logger、Handler、Filter、Formatter。Logger對象提供應用程序可直接使用的接口,Handler發送日誌到適當的目的地,Filter提供了過濾日誌信息的方法,Formatter指定日誌顯示格式。另外,能夠經過:logger.setLevel(logging.Debug)設置級別,固然,也能夠經過fh.setLevel(logging.Debug)單對文件流設置某個級別。
""" logging配置 """ import os import logging.config # 定義三種日誌輸出格式 開始 standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ '[%(levelname)s][%(message)s]' #其中name爲getlogger指定的名字 simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s' # 定義日誌輸出格式 結束 logfile_dir = os.path.dirname(os.path.abspath(__file__)) # log文件的目錄 logfile_name = 'all2.log' # log文件名 # 若是不存在定義的日誌目錄就建立一個 if not os.path.isdir(logfile_dir): os.mkdir(logfile_dir) # log文件的全路徑 logfile_path = os.path.join(logfile_dir, logfile_name) # log配置字典 LOGGING_DIC = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': standard_format }, 'simple': { 'format': simple_format }, }, 'filters': {}, 'handlers': { #打印到終端的日誌 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', # 打印到屏幕 'formatter': 'simple' }, #打印到文件的日誌,收集info及以上的日誌 'default': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 'formatter': 'standard', 'filename': logfile_path, # 日誌文件 'maxBytes': 1024*1024*5, # 日誌大小 5M 'backupCount': 5, 'encoding': 'utf-8', # 日誌文件的編碼,不再用擔憂中文log亂碼了 }, }, 'loggers': { #logging.getLogger(__name__)拿到的logger配置 '': { 'handlers': ['default', 'console'], # 這裏把上面定義的兩個handler都加上,即log數據既寫入文件又打印到屏幕 'level': 'DEBUG', 'propagate': True, # 向上(更高level的logger)傳遞 }, }, } def load_my_logging_cfg(): logging.config.dictConfig(LOGGING_DIC) # 導入上面定義的logging配置 logger = logging.getLogger(__name__) # 生成一個log實例 logger.info('It works!') # 記錄該文件的運行狀態 if __name__ == '__main__': load_my_logging_cfg()
注意: #一、有了上述方式咱們的好處是:全部與logging模塊有關的配置都寫到字典中就能夠了,更加清晰,方便管理 #二、咱們須要解決的問題是: 一、從字典加載配置:logging.config.dictConfig(settings.LOGGING_DIC) 二、拿到logger對象來產生日誌 logger對象都是配置到字典的loggers 鍵對應的子字典中的 按照咱們對logging模塊的理解,要想獲取某個東西都是經過名字,也就是key來獲取的 因而咱們要獲取不一樣的logger對象就是 logger=logging.getLogger('loggers子字典的key名') 但問題是:若是咱們想要不一樣logger名的logger對象都共用一段配置,那麼確定不能在loggers子字典中定義n個key 'loggers': { 'l1': { 'handlers': ['default', 'console'], # 'level': 'DEBUG', 'propagate': True, # 向上(更高level的logger)傳遞 }, 'l2: { 'handlers': ['default', 'console' ], 'level': 'DEBUG', 'propagate': False, # 向上(更高level的logger)傳遞 }, 'l3': { 'handlers': ['default', 'console'], # 'level': 'DEBUG', 'propagate': True, # 向上(更高level的logger)傳遞 }, } #咱們的解決方式是,定義一個空的key 'loggers': { '': { 'handlers': ['default', 'console'], 'level': 'DEBUG', 'propagate': True, }, } 這樣咱們再取logger對象時 logging.getLogger(__name__),不一樣的文件__name__不一樣,這保證了打印日誌時標識信息不一樣,可是拿着該名字去loggers裏找key名時卻發現找不到,因而默認使用key=''的配置
在內置數據類型(dict、list、set、tuple)的基礎上,collections模塊還提供了幾個額外的數據類型:Counter、deque、defaultdict、namedtuple和OrderedDict等。
from collections import namedtuple point = namedtuple('point',['x','y']) p = point(1,2) print(p.x)
一個點的二維座標就能夠表示成,可是,看到(1, 2),很難看出這個tuple是用來表示一個座標的。
這時,namedtuple就派上了用場
使用list存儲數據時,按索引訪問元素很快,可是插入和刪除元素就很慢了,由於list是線性存儲,數據量大的時候,插入和刪除效率很低。
deque是爲了高效實現插入和刪除操做的雙向列表,適合用於隊列和棧
from collections import deque q = deque(['a','b','c']) q.append('x') q.appendleft('y') print(q)
deque除了實現list的append()和pop()外,還支持appendleft()和popleft(),這樣就能夠很是高效地往頭部添加或刪除元素。
from collections import OrderedDict d = dict([('a',1),('b',2),('c',3)]) print(d) od = OrderedDict([('a',1),('b',2),('c',3)]) print(od)
注意,OrderedDict的Key會按照插入的順序排列,不是Key自己排序
有以下值集合 [11,22,33,44,55,66,77,88,99,90...],將全部大於 66 的值保存至字典的第一個key中,將小於 66 的值保存至第二個key的值中。
即: {'k1': 大於66 , 'k2': 小於66}
li = [11,22,33,44,55,77,88,99,90] result = {} for row in li: if row < 66: if 'key1' not in result: result['key1']=[] result['key1'].append(row) else: if 'key2' not in result: result['key2']=[] result['key2'].append(row) print(result)
from collections import defaultdict li = [11,22,33,44,55,77,88,99,90] result=defaultdict(list) for row in li: if row > 66: result['key1'].append(row) else: result['key2'].append(row) print(result)
Counter類的目的是用來跟蹤值出現的次數。它是一個無序的容器類型,以字典的鍵值對形式存儲,其中元素做爲key,其計數做爲value。
from collections import Counter c = Counter('qazxswqazxswqazxswsxaqwsxaqws') print(c)
經常使用方法
表示時間的三種方式
在Python中,一般有這三種方式來表示時間:時間戳、元組(struct_time)、格式化的時間字符串:
%y | 兩位數的年份表示(00-99) |
---|---|
%Y | 四位數的年份表示(000-9999) |
%m | 月份(01-12) |
%d | 月內中的一天(0-31) |
%H | 24小時制小時數(0-23) |
%I | 12小時制小時數(01-12) |
%M | 分鐘數(00=59) |
%S | 秒(00-59) |
%a | 本地簡化星期名稱 |
%A | 本地完整星期名稱 |
%b | 本地簡化的月份名稱 |
%B | 本地完整的月份名稱 |
%c | 本地相應的日期表示和時間表示 |
%j | 年內的一天(001-366) |
%p | 本地A.M.或P.M.的等價符 |
%U | 一年中的星期數(00-53)星期天爲星期的開始 |
%w | 星期(0-6),星期天爲星期的開始 |
%W | 一年中的星期數(00-53)星期一爲星期的開始 |
%x | 本地相應的日期表示 |
%X | 本地相應的時間表示 |
%Z | 當前時區的名稱 |
%% | %號自己 |
索引(Index) | 屬性(Attribute) | 值(Values) |
---|---|---|
0 | tm_year(年) | 好比2011 |
1 | tm_mon(月) | 1月12日 |
2 | tm_mday(日) | 1月31日 |
3 | tm_hour(時) | 0 - 23 |
4 | tm_min(分) | 0 - 59 |
5 | tm_sec(秒) | 0 - 60 |
6 | tm_wday(weekday) | 0 - 6(0表示週一) |
7 | tm_yday(一年中的第幾天) | 1 - 366 |
8 | tm_isdst(是不是夏令時) | 默認爲0 |
import time print(time.time()) print(time.strftime('%Y-%m-%d %X')) print(time.strftime('%Y-%m-%d %H-%M-%S')) print(time.localtime())
小結:時間戳是計算機可以識別的時間;時間字符串是人可以看懂的時間;元組則是用來操做時間的
幾種格式之間的轉換
import time # 格式化時間 ----> 結構化時間 ft = time.strftime('%Y/%m/%d %H:%M:%S') st = time.strptime(ft,'%Y/%m/%d %H:%M:%S') print(st) # 結構化時間 ---> 時間戳 t = time.mktime(st) print(t) # 時間戳 ----> 結構化時間 t = time.time() st = time.localtime(t) print(st) # 結構化時間 ---> 格式化時間 ft = time.strftime('%Y/%m/%d %H:%M:%S',st) print(ft)
import time #結構化時間 --> %a %b %d %H:%M:%S %Y串 #time.asctime(結構化時間) 若是不傳參數,直接返回當前時間的格式化串 print(time.asctime(time.localtime(1550312090.4021888))) #時間戳 --> %a %d %d %H:%M:%S %Y串 #time.ctime(時間戳) 若是不傳參數,直接返回當前時間的格式化串 print(time.ctime(1550312090.4021888))
計算時間差
import time start_time=time.mktime(time.strptime('2017-09-11 08:30:00','%Y-%m-%d %H:%M:%S')) end_time=time.mktime(time.strptime('2019-09-12 11:00:50','%Y-%m-%d %H:%M:%S')) dif_time=end_time-start_time struct_time=time.gmtime(dif_time) print('過去了%d年%d月%d天%d小時%d分鐘%d秒'%(struct_time.tm_year-1970,struct_time.tm_mon-1, struct_time.tm_mday-1,struct_time.tm_hour, struct_time.tm_min,struct_time.tm_sec))
# datatime模塊 import datetime now_time = datetime.datetime.now() # 如今的時間 # 只能調整的字段:weeks days hours minutes seconds print(datetime.datetime.now() + datetime.timedelta(weeks=3)) # 三週後 print(datetime.datetime.now() + datetime.timedelta(weeks=-3)) # 三週前 print(datetime.datetime.now() + datetime.timedelta(days=-3)) # 三天前 print(datetime.datetime.now() + datetime.timedelta(days=3)) # 三天後 print(datetime.datetime.now() + datetime.timedelta(hours=5)) # 5小時後 print(datetime.datetime.now() + datetime.timedelta(hours=-5)) # 5小時前 print(datetime.datetime.now() + datetime.timedelta(minutes=-15)) # 15分鐘前 print(datetime.datetime.now() + datetime.timedelta(minutes=15)) # 15分鐘後 print(datetime.datetime.now() + datetime.timedelta(seconds=-70)) # 70秒前 print(datetime.datetime.now() + datetime.timedelta(seconds=70)) # 70秒後 current_time = datetime.datetime.now() # 可直接調整到指定的 年 月 日 時 分 秒 等 print(current_time.replace(year=1977)) # 直接調整到1977年 print(current_time.replace(month=1)) # 直接調整到1月份 print(current_time.replace(year=1989,month=4,day=25)) # 1989-04-25 18:49:05.898601 # 將時間戳轉化成時間 print(datetime.date.fromtimestamp(1232132131)) # 2009-01-17
import random print(random.random()) # 大於0且小於1之間的小數 print(random.uniform(1,3)) # 大於1小於3的小數 print(random.randint(1,5)) # 大於等於1且小於等於5之間的整數 print(random.randrange(1,10,2)) # 大於等於1且小於10之間的奇數 ret = random.choice([1,'23',[4,5]]) # 1或者23或者[4,5] print(ret) a,b = random.sample([1,'23',[4,5]],2) # 列表元素任意2個組合 print(a,b) item = [1,3,5,7,9] random.shuffle(item) # 打亂次序 print(item)
生成隨機驗證碼
import random def v_code(): code = '' for i in range(5): num=random.randint(0,9) alf=chr(random.randint(65,90)) add=random.choice([num,alf]) code="".join([code,str(add)]) return code print(v_code())
os模塊是與操做系統交互的一個接口
當前執行這個python文件的工做目錄相關的工做路徑
os.getcwd() | 獲取當前工做目錄,即當前python腳本工做的目錄路徑 |
---|---|
os.chdir("dirname") | 改變當前腳本工做目錄;至關於shell下cd |
os.curdir | 返回當前目錄: ('.') |
os.pardir | 獲取當前目錄的父目錄字符串名:('..') |
文件夾相關
os.makedirs('dirname1/dirname2') | 可生成多層遞歸目錄 |
---|---|
os.removedirs('dirname1') | 若目錄爲空,則刪除,並遞歸到上一級目錄,如若也爲空,則刪除,依此類推 |
os.mkdir('dirname') | 生成單級目錄;至關於shell中mkdir dirname |
os.rmdir('dirname') | 刪除單級空目錄,若目錄不爲空則沒法刪除,報錯;至關於shell中rmdir dirname |
os.listdir('dirname') | 列出指定目錄下的全部文件和子目錄,包括隱藏文件,並以列表方式打印 |
文件相關
os.remove() | 刪除一個文件 |
---|---|
os.rename("oldname","newname") | 重命名文件/目錄 |
os.stat('path/filename') | 獲取文件/目錄信息 |
操做系統差別相關
os.sep | 輸出操做系統特定的路徑分隔符,win下爲"\\",Linux下爲"/" |
---|---|
os.linesep | 輸出當前平臺使用的行終止符,win下爲"\t\n",Linux下爲"\n" |
os.pathsep | 輸出用於分割文件路徑的字符串 win下爲;,Linux下爲: |
os.name | 輸出字符串指示當前使用平臺。win->'nt'; Linux->'posix' |
執行系統命令相關
os.system("bash command") | 運行shell命令,直接顯示 |
---|---|
os.popen("bash command).read() | 運行shell命令,獲取執行結果 |
os.environ | 獲取系統環境變量 |
path系列,和路徑相關
os.path.abspath(path) | 返回path規範化的絕對路徑 |
---|---|
os.path.split(path) | 將path分割成目錄和文件名二元組返回 |
os.path.dirname(path) | 返回path的目錄。其實就是os.path.split(path)的第一個元素 |
os.path.basename(path) | 返回path最後的文件名。如何path以/或\結尾,那麼就會返回空值,即os.path.split(path)的第二個元素。 |
os.path.exists(path) | 若是path存在,返回True;若是path不存在,返回False |
os.path.isabs(path) | 若是path是絕對路徑,返回True |
os.path.isfile(path) | 若是path是一個存在的文件,返回True。不然返回False |
os.path.isdir(path) | 若是path是一個存在的目錄,則返回True。不然返回False |
os.path.join(path1[, path2[, ...]]) | 將多個路徑組合後返回,第一個絕對路徑以前的參數將被忽略 |
os.path.getatime(path) | 返回path所指向的文件或者目錄的最後訪問時間 |
os.path.getmtime(path) | 返回path所指向的文件或者目錄的最後修改時間 |
os.path.getsize(path) | 返回path的大小 |
import os print(os.stat('.\config')) # 當前目錄下的config文件的信息 # 運行結果 # os.stat_result(st_mode=33206, st_ino=2814749767208887, st_dev=1788857329, st_nlink=1, st_uid=0, st_gid=0, st_size=185, st_atime=1550285376, st_mtime=1550285376, st_ctime=1550285376)
st_mode | inode 保護模式 |
---|---|
st_ino | inode 節點號 |
st_dev | inode 駐留的設備 |
st_nlink | inode 的連接數 |
st_uid | 全部者的用戶ID |
st_gid | 全部者的組ID |
st_size | 普通文件以字節爲單位的大小;包含等待某些特殊文件的數據 |
st_atime | 上次訪問的時間 |
st_mtime | 最後一次修改的時間 |
st_ctime | 由操做系統報告的"ctime"。在某些系統上(如Unix)是最新的元數據更改的時間,在其它系統上(如Windows)是建立時間(詳細信息參見平臺的文檔) |
sys模塊是與python解釋器交互的一個接口
sys.argv | 命令行參數List,第一個元素是程序自己路徑 |
---|---|
sys.exit(n) | 退出程序,正常退出時exit(0),錯誤退出sys.exit(1) |
sys.version | 獲取Python解釋程序的版本信息 |
sys.path | 返回模塊的搜索路徑,初始化時使用PYTHONPATH環境變量的值 |
sys.platform | 返回操做系統平臺名稱 |
正則就是用一些具備特殊含義的符號組合到一塊兒(稱爲正則表達式)來描述字符或者字符串的方法。或者說:正則就是用來描述一類事物的規則。(在Python中)它內嵌在Python中,並經過 re 模塊實現。正則表達式模式被編譯成一系列的字節碼,而後由用 C 編寫的匹配引擎執行。
元字符 | 匹配內容 |
---|---|
\w | 匹配字母(包含中文)或數字或下劃線 |
\W | 匹配非字母(包含中文)或數字或下劃線 |
\s | 匹配任意的空白符 |
\S | 匹配任意非空白符 |
\d | 匹配數字 |
\D | 匹配非數字 |
\A | 從字符串開頭匹配 |
\z | 匹配字符串的結束,若是是換行,只匹配到換行前的結果 |
\n | 匹配一個換行符 |
\t | 匹配一個製表符 |
^ | 匹配字符串的開始 |
$ | 匹配字符串的結尾 |
. | 匹配任意字符,除了換行符,當re.DOTALL標記被指定時,則能夠匹配包括換行符的任意字符。 |
[...] | 匹配字符組中的字符 |
[^...] | 匹配除了字符組中的字符的全部字符 |
* | 匹配0個或者多個左邊的字符。 |
+ | 匹配一個或者多個左邊的字符。 |
? | 匹配0個或者1個左邊的字符,非貪婪方式。 |
{n} | 精準匹配n個前面的表達式。 |
{n,m} | 匹配n到m次由前面的正則表達式定義的片斷,貪婪方式 |
a | b |
() | 匹配括號內的表達式,也表示一個組 |
import re print(re.findall('\w','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('\W','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('\s','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('\S','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('\d','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('\D','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('\A上大','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('^上大','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('666\z','上大人123asdfg%^&*(_ \t \n)666')) print(re.findall('666\Z','上大人123asdfg%^&*(_ \t \n)666')) print(re.findall('666$','上大人123asdfg%^&*(_ \t \n)666')) print(re.findall('\n','上大人123asdfg%^&*(_ \t \n)')) print(re.findall('\t','上大人123asdfg%^&*(_ \t \n)'))
import re print(re.findall('a.b', 'ab aab a*b a2b a牛b a\nb')) print(re.findall('a.b', 'ab aab a*b a2b a牛b a\nb',re.DOTALL)) print(re.findall('a?b', 'ab aab abb aaaab a牛b aba**b')) print(re.findall('a*b', 'ab aab aaab abbb')) print(re.findall('ab*', 'ab aab aaab abbbbb')) print(re.findall('a+b', 'ab aab aaab abbb')) print(re.findall('a{2,4}b', 'ab aab aaab aaaaabb')) print(re.findall('a.*b', 'ab aab a*()b')) print(re.findall('a.*?b', 'ab a1b a*()b, aaaaaab')) # .*? 此時的?不是對左邊的字符進行0次或者1次的匹配, # 而只是針對.*這種貪婪匹配的模式進行一種限定:告知他要聽從非貪婪匹配 推薦使用! # []: 括號中能夠聽任意一個字符,一箇中括號表明一個字符 # - 在[]中表示範圍,若是想要匹配上- 那麼這個-符號不能放在中間. # ^ 在[]中表示取反的意思. print(re.findall('a.b', 'a1b a3b aeb a*b arb a_b')) print(re.findall('a[abc]b', 'aab abb acb adb afb a_b')) print(re.findall('a[0-9]b', 'a1b a3b aeb a*b arb a_b')) print(re.findall('a[a-z]b', 'a1b a3b aeb a*b arb a_b')) print(re.findall('a[a-zA-Z]b', 'aAb aWb aeb a*b arb a_b')) print(re.findall('a[0-9][0-9]b', 'a11b a12b a34b a*b arb a_b')) print(re.findall('a[*-+]b','a-b a*b a+b a/b a6b')) print(re.findall('a[-*+]b','a-b a*b a+b a/b a6b')) print(re.findall('a[^a-z]b', 'acb adb a3b a*b')) # 分組:() 制定一個規則,將知足規則的結果匹配出來 print(re.findall('(.*?)_sb', 'cs_sb zhao_sb 日天_sb')) print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">點擊</a>')) print(re.findall('compan(y|ies)','Too many companies have gone bankrupt, and the next one is my company')) print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company')) # 分組() 中加入?: 表示將總體匹配出來而不僅是()裏面的內容
import re # findall 所有找到返回一個列表 print(re.findall('a','aghjmnbghagjmnbafgv')) # search 只到找到第一個匹配而後返回一個包含匹配信息的對象,該對象能夠經過調用group()方法獲得匹配的字符串,若是字符串沒有匹配,則返回None print(re.search('sb|chensong', 'chensong sb sb demon 日天')) print(re.search('chensong', 'chensong sb sb barry 日天').group()) # match:None,同search,不過在字符串開始處進行匹配,徹底能夠用search+^代替match print(re.match('sb|chensong', 'chensong sb sb demon 日天')) print(re.match('chensong', 'chensong sb sb barry 日天').group()) # split 分割 可按照任意分割符進行分割 print(re.split('[::,;;,]','1;3,c,a:3')) # sub 替換 print(re.sub('帥哥','sb','陳鬆是一個帥哥')) # complie 根據包含的正則表達式的字符串建立模式對象。能夠實現更有效率的匹配。 obj = re.compile('\d{2}') print(obj.search('abc123eeee').group()) print(obj.findall('1231232aasd')) ret = re.finditer('\d','asd123affess32432') # finditer返回一個存放匹配結果的迭代器 print(ret) print(next(ret).group()) print(next(ret).group()) print([i.group() for i in ret])
命名分組匹配
import re ret = re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>") print(ret.group('tag_name')) print(ret.group()) ret = re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>") # 若是不給組起名字,也能夠用\序號來找到對應的組,表示要找的內容和前面的組內容一致 # 獲取的匹配結果能夠直接用group(序號)拿到對應的值 print(ret.group(1)) print(ret.group())
高級的 文件、文件夾、壓縮包 處理模塊
將文件內容拷貝到另外一個文件中
import shutil shutil.copyfileobj(open('config','r'),open('config.new','w'))
拷貝文件
import shutil shutil.copyfile('config','config1') # 目標文件無需存在
僅拷貝權限。內容、組、用戶均不變
import shutil shutil.copymode('config','config1') # 目標文件必須存在
僅拷貝狀態的信息,包括:mode bits, atime, mtime, flags
import shutil shutil.copystat('config','config1') # 目標文件必須存在
拷貝文件和權限
import shutil shutil.copy('config','config1') # 目標文件必須存在
遞歸的去拷貝文件夾
import shutil shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) # 目標目錄不能存在,注意對folder2目錄父級目錄要有可寫權限,ignore的意思是排除 # 硬連接 shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) # 軟連接
遞歸的去刪除文件
import shutil shutil.rmtree('folder1')
遞歸的去移動文件,它相似mv命令,其實就是重命名。
import shutil shutil.move('folder1', 'folder3')
# 將 /data 下的文件打包放置當前程序目錄 import shutil ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data') # 將 /data下的文件打包放置 /tmp/目錄 import shutil ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模塊來進行的
import zipfile # 壓縮 z = zipfile.ZipFile('laxi.zip', 'w') z.write('a.log') z.write('data.data') z.close() # 解壓 z = zipfile.ZipFile('laxi.zip', 'r') z.extractall(path='.') z.close()
import tarfile t = tarfile.open('/tmp/egon.tar','w') t.add('/test1/a.py',arcname='a.bak') t.add('/test1/b.py',arcname='b.bak') t.close() t = tarfile.open('/tmp/egon.tar','r') t.extractall('/egon') t.close()