1. 程式人生 > >python中常用模塊2

python中常用模塊2

數據交換 特殊 str null filter 即將 結構 spl all

time模塊即1與時間有關的模塊

可以分為三類:

1.時間戳 從1970-1-1 0:0:0開始計算時間

time.time()

2.結構化時間

time.localtime() 返回的是一個元祖 是指當地的時間

time.localtime()也可以獲取單個值 例如:time.localtime().tm_year

time.gmtime() 指時間統一時間

3.格式化的字符串時間

time.strftime("%Y-%m-%d %H:%M:%S %p")

time.strftime("%Y-%m-%d %X %p")

三種格式之間也可相互轉化

和時間相關的常用模塊還有datetime模塊,datetime模塊可以分成兩個部分1.和日期相關2.和時間相關

datetime模塊

datetime.datetime.now() 獲取當前的詳細時間,也可以獲取時間的部分

例如:datetime.datetime.now().hour

亦可以替換時間的某一部分

例如:x=datetime.datetime.now()

x1=x.replace(year=2020)

print(x)

這樣就可以把年份換掉了

datetime.timedelta() 時間差對象不常用

pickle模塊

pickle是一個用來序列化的模塊,序列化即將內存中的數據結構轉化為一種中間格式存儲到硬盤中,永久存儲.說道序列化就要提到反序列化,即把硬盤上存儲的中間格式數據還原到內存中使用.

pickle.dump()和pickle.dumps()都是序列化.區別在與pickle.dump對write進行了封裝

pickle.load()和pickle.loads()都是反序列化.區別是pickle.load對read進行了封裝.

shelve模塊

也是用於序列化的,和pickle的不同在於不需要關心文件模式,直接把它當成字典看待,也可以直接對數據進行修改,而不用覆蓋原有的數據.

json模塊

json是java script object notation的縮寫對於python來說json就是一種通用的數據格式

js 中的數據類型 python數據類型 的對應關系
{} 字典
[] list
string "" str
int/float int/float
true/false True/False
null None

json的常用功能

json.dump()和json.dumps()

json.load()和json.loads()

xml模塊

xml指可擴展的標記語言

xml.etree.ElementTree.parse() 解析文件

rootTree = tree.getroot() 獲取根便簽

for item in rootTree.iter() 獲取全文內所有標簽

item.tag() 獲取標簽名

item.attrib() 獲取標簽的屬性

item.text() 獲取文本的內容

rootTree.find() 獲取單個屬性

rootTree.remove() 刪除標簽

添加標簽

x=xml.etree.ElementTree.Element()

rootTree.append(x)

tree.write() 寫入文件

configparser模塊

用於解析配置文件的模塊,配置文件只有兩種內容:section和option

configparser.ConfigParser() 創建一個解釋器

config.read() 讀取並解析文件

config.sections() 獲取所有分區

config.options() 獲取所有選項

config.get(sections,options) 獲取某個值,返回的是字符串類型的值

config.has_option() 是否存在某個選項

config.add_section() 添加分區

config.set()

config.remove_option() 刪除選項

config.set() 可以用於修改

config.write() 寫入文件

logging模塊

1. 日誌的級別

loging.debug 調試信息 10

logging.info 常規信息 20

logging.warning 警告信息 30

logging.error 錯誤信息 40

logging.critical 嚴重錯誤 50

默認情況下 級別是30 logging warning 日誌的輸入位置是控制臺

2.自定義日誌的配置

logging.basicConfig(

filename="a.log",
filemode="at",
level=10,
format="%(asctime)s %(levelname)s %(funcName)s %(lineno)s %(message)s",
datefmt="%Y-%m-%d %X %p"
)

日誌模塊的四個核心角色

1.logger 2.filter 3.handler 4. formatter

1.生成一個日誌

logger=logging.getLogger(‘logger‘)

.設置日誌級別

logger.setLever(logging.DEBUG)

2.日誌處理器

logging.FileHandler()

3.格式化處理器

logging.Formatter("%(asctime)s %(levelname)s %(funcName)s %(lineno)s %(message)s",datefmt="%Y-%m-%d %X %p")

4.將以上三步進行連接

logger文件的繼承

以字典生成logger

logging.StreamHandler() 輸出到控制臺

logging.FileHandler() 輸出到文件

hashlib模塊

hash是一種算法,將 一的個任意長的數據得到固定長度特征碼.

重要用於密碼驗證

m=hashlib.md5("aaa".encode("utf-8"))

加鹽

m.update("abcdefplkjoujhh".encode("utf-8"))

print(m.hexdigest)

re模塊

主要正則表達式相關即 一堆帶有特殊意義的符號組成式子

技術分享圖片

re.findall() 找出所有

re.match 從字符串開始處匹配,只找一個

re.search() 從全文範圍匹配,只取一個

re.split() 切分

re.sub() 替換

當要匹配的內容包含\時

\\\\

subprocess模塊

res=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

stdout 輸出管道

stdin 輸入管道

stderr 錯誤管道

res.stdout.read()

subprocess 主要用於執行系統指令 (啟動子進程) 與os.system的不同在於
subprocess 可以與這個子進程進行數據交換

python中常用模塊2