6. Python 模塊
模塊其實就是一個python文件
python導入模塊的順序
1. 從當前目錄下找需要導入的python文件
2. 從python的環境變量中找 sys.path
當前目錄和sys.path中都有import文件時,優先看當前目錄的python文件。
1、標準模塊(標準包):python自帶的模塊,直接import就能用的
string , random ,datetime ,os, json
2、第三方模塊,別人寫好的一些模塊,要安裝之后才可以用
1)傻瓜式安裝
pip install pymysql
2)手動安裝
到百度搜索安裝包,如安裝redis.
whl結尾的安裝包
下載完成后,在下載文件夾的地址欄中輸入cmd,即可打開當前目錄命令行,輸入命令:pip install redis-2.10.6-py2.py3-none-any.whl
.tar.gz結尾的安裝包
如果是下載的redis-2.10.6.tar.gz,解壓后,進入目錄,在地址欄輸入cmd
在命令行運行:python setup.py install
3、自己寫的python文件,當作一個模塊來引用
random模塊
import random,string
print(string.printable) #代表 數字+字母+特殊字符
print(random.randint(1,100)) #隨機取整數
print(round(random.uniform(1,99),2)) #取隨機小數,加round取2位小數
print(random.choice([1,2,3,4])) #只能隨機取1個元素
print(random.sample(string.printable,5)) #隨機取N個元素,返回的是list
#洗牌
pickts = ['A','J','Q','K',2,3,4,5,6]
random.shuffle(pickts) #只能傳list
print(pickts)
json模塊
json串格式校驗:http://www.bejson.com
1、json串 轉成一個字典:load
import json
#json串是一個字符串
f = open('product.json',encoding='utf-8')
res = f.read() #用json.loads()要先讀一次,變成字符串
product_dic = json.loads(res) #json.loads() 只能傳一個字符串,把json串變成python的數據類型:字典
print(type(product_dic)) #已轉換成字典類型
print(product_dic)
print(product_dic.get('product_info'))
import json
#json串是一個字符串
f = open('product.json',encoding='utf-8')
print(json.load(f)) #只傳一個文件對象,自動讀取文件,直接操作文件
2、字典轉換一個json串:dump
d = {
"iphone":{
"color":"red",
"num":1,
"price":98.5
},
"wather":{
"num":100,
"price":1,
"color":"white"
}
}
import json
fw = open('product.json', 'w', encoding='utf-8')
dic_json = json.dumps(d, ensure_ascii=False, indent=4) #字典轉換成json,變成字符串,ensure_ascii=False 顯示中文,INDENT = indent = 4
fw.write(dic_json)
也可以寫成:
import json
fw = open('product.json', 'w', encoding='utf-8')
json.dump(d,fw,ensure_ascii=False,indent=4)
dump是直接對文件進行操作,dumps是對字典進行操作
時間模塊
#時間戳:
#1. 從unix元年到現在過了多少秒
#2.格式化好的時間
import time
print(time.time())
# time.sleep(1)
today = time.strftime('%Y-%m-%d %H:%M:%S')
# today = time.strftime('%y-%m-%d %H:%M:%S')
print(today)
#時間戳 轉換 時間:
# 1. 要先轉換成時間元組
#2.再把時間元組轉成成格式化的時間
print(time.gmtime()) #默認取的是標準時區的時間,不傳時默認標準時區的時間戳
print(time.localtime()) #默認取的是當前時區的時間,不傳時默認當前時區的時間戳
s = time.localtime(1524299155)
print(time.strftime('%Y-%m-%d %H:%M:%S',s))
時間戳轉換格式化時間的函數
def timestamp_to_fomat(timestamp = None,format = '%Y-%m-%d %H:%M:%S'):
if timestamp:
time_tuple = time.localtime(timestamp)
res = time.strftime(format,time_tuple)
else:
res = time.strftime(format)
return res
print(timestamp_to_fomat())
print(timestamp_to_fomat(19732649413))
# 時間轉換成時間戳
#1.把格式化的時間轉換成時間元組
#2.把元組轉換成時間戳
res = time.strptime('20180421','%Y%m%d')
print(res)
print(time.mktime(res))
格式化時間轉換時間戳的函數:
# 時間轉換成時間戳
#1.把格式化的時間轉換成時間元組
#2.把元組轉換成時間戳
res = time.strptime('20180421','%Y%m%d')
print(res)
print(time.mktime(res))
def strToTimestamp(str = None,format = '%Y%m%d'):
if str:
tp = time.strptime(str,format)
res = time.mktime(tp)
else:
res = time.time()
return int(res)
print(strToTimestamp())
print(strToTimestamp('2018-02-15','%Y-%m-%d'))
datetime 模塊
import datetime
print(datetime.datetime.today()) #獲取當前時間,精確到秒
print(datetime.date.today()) #精確到天
day = datetime.date.today() + datetime.timedelta(days=5)
print(day)
min = datetime.datetime.today() + datetime.timedelta(days=1,minutes=5,seconds=5,weeks=1)
print(min)
print(min.strftime('%Y-%m-%d'))
加密模塊
import hashlib
m = hashlib.md5()
# print(m.__doc__) #查看有哪些方法
print(m.hexdigest()) #對空加密
passwd = 'aaa123'
print(passwd.encode()) #把字符串轉換成bytes類型,加密不能對字符串進行加密,要轉換成bytes類型
m.update(passwd.encode())
print(m.hexdigest())
#md5加密是不可逆的
網上md5解密是采用的撞庫來查詢的。
def my_md5(srt):
import hashlib
new_str = str.encode() #把字符串轉換層buytes類型
m = hashlib.md5() #實例化md5對象
m.update(new_str) #加密
return m.hexdigest() #獲取結果返回

浙公網安備 33010602011771號