#map()和filter循環幫你調用函數的php
import os
import time
def makdir(dir_name):
if not os.path.isdir(dir_name):
os.mkdir(dir_name)
return True
dir_names = ['android','ios','java','tomcat','python','php']
res = map(makdir,dir_names) #循環幫你調用函數
res1= list(map(makdir,dir_names))
print(res) #返回的是一個生成器
print(res1) #返回一個list
def timestampToStr(timestamp=None,format='%Y%m%d'):
if timestamp:
time_tuple = time.localtime(timestamp) #轉成時間元組
return time.strftime(format,time_tuple)
return time.strftime(format)
all_data = [timestampToStr(int(time.time())-86400*i) for i in range(10)]
print(all_data) #這個就是獲取到的是一個list
all_data1 = (timestampToStr(int(time.time())-86400*i) for i in range(10))
print(all_data1) #使用小括號獲取的是一個生成器
for a in all_data1: #循環該生成器也能夠獲取到對應的數據
print(a)
#生成器
# 生成器爲了節省內存的,每次循環的時候,按照這個規則(你寫的邏輯)去生成一個數據,但增長了cpu的計算時間
#
#生成器示例
nums = (str(i).zfill(2) for i in range(10))
print(nums)
for n in nums:
print(n)
range(10) #range函數其實也是一個生成器,在循環的才能獲取到對應的數據
for i in range:
print(range)java
#filter做用是過濾,把函數處理結果爲假的數據給過濾掉,只保留函數返回真的數據python
def my(num):
if num%2==0:
return True
res = list(filter(my,range(10)))
res2 = list(map(my,range(10)))
print('res',res) #filter做用是過濾,把函數處理結果爲假的數據給過濾掉,只保留函數返回真的數據
print('res2',res2) #map的做用是無論你返回啥,都給獲取到mysql
import os
print(os.path.abspath('..\\day5')) #取絕對路徑
print(os.getcwd()) #取當前路徑
#.表明當前目錄,..表明上一級目錄
print(os.listdir('D:\\pythonscript\\day5'))
os.chdir('D:\\pythonscript\\day6') #更改當前工做目錄
print(os.getcwd())
linux
用來執行操做系統命令,可是隻能幫你執行,獲取不到命令執行的結果android
res = os.system('dir') #用來執行操做系統命令,可是隻能幫你執行,獲取不到命令執行的結果
print('res',res) #若是返回結果是0,表示命令執行成功,返回1表示執行失敗ios
也是用來執行操做系統命令,可是它能夠獲取到命令執行的結果
res = os.popen('dir').read() #後面添加個read方法就能夠獲取到命令執行的結果
print('popen',res)redis
此方法只能獲取靜態的數據,如linux下的要想獲取top這個實時動態的數據則沒法獲取到,要想獲取到則經過使用top –n 1來實現獲取一次top的數據便可。算法
import datetime
print(datetime.date.today()) #獲取到當天的日期(年月日)
print(datetime.datetime.today()) #獲取到當前的時間(年月日及具體時間)
t1 = datetime.date.today() + datetime.timedelta(-1) #獲取昨天的數據
t2 = datetime.date.today() + datetime.timedelta(days=1) #獲取明天的數據,days=能夠不加
print(t1,t2)
t3 = datetime.datetime.today() + datetime.timedelta(days=1,hours=10,minutes =20,seconds=5) #當前的時間往前或日後推多長時間
print(t3)
print(t3.time()) #只取到時間
print(t3.date()) #只取到日期
print(t3.timestamp()) #取到時間戳
print(t3.strftime('%Y-%m-%d')) #取到格式化時間sql
import random
print(random.random()) #取小於1的隨機的小數
print(random.randint(1,10)) #指定範圍,取隨機的整數,如範圍取1-10之間的整數,顧頭也顧尾
s = 'abcd'
print(random.choice(s)) #隨機選擇一個,只能選擇一個,能夠是字符串,list、字典、集合、元組等
print(random.sample(s,3)) #隨機選擇N個,返回的是一個list,如3表示隨機選擇3個,隨機選擇的不會重複
print(random.uniform(1,10)) #指定一個範圍,而後取一個隨機小數
import nnlog
my_log = nnlog.Logger('test1.log',when='S',backCount=5) #when是按天生成文件,將D修改成S則按秒生成文件,backCount備份文件最多隻備份5個
#日誌級別
#debug,級別最低,打印的信息最詳細
#info
#warning
#error
my_log.debug('debug級別')
my_log.info('info級別')
my_log.warning('warning級別')
my_log.error('error級別')
import yagmail
#帳號密碼,郵箱服務器,收件人,抄送人,主題,內容,附件
username = 'aaaa@126.com'
passwd = 'bbbb' #此處須要使用郵箱的受權碼,受權碼須要在郵件中進行設置
#smtp_ssl=True 安全協議,若是是QQ郵箱須要加上這個參數,網易郵箱能夠不加
mail = yagmail.SMTP(user=username,password=passwd,host='smtp.126.com') #連上郵箱
mail.send(to=['aaa@qq.com','bbb@qq.com'], #發送郵件若是是有多個收件人則直接用list便可
cc='ccc@163.com',
subject='發送帶附件郵件測試',
contents='郵件發送成功',
attachments=r'D:\pythonscript\day6\發郵件.py') #多個附件寫個list便可,若是郵件附件中文亂碼的話從新安裝一下牛牛修改的whl的yagmail包,不是亂碼的不用管
#連上數據庫ip 帳號密碼,端口號,數據庫,執行sql,獲取到結果
import pymysql
conn = pymysql.connect(host='1.1.1.1',user='jxz',password='123456',port=3306,db='jxz',charset='utf8',autocommit=True) #鏈接數據庫
cur = conn.cursor() #創建遊標
# cur.execute('select * from nhy;') #執行sql語句,只是執行sql語句,不會返回數據
sql = 'insert into nhy (name,pwd) value ("zhangsan","123456");'
cur.execute(sql)
# conn.commit() #執行insert、update、delete語句時須要提交一下才能插入到數據庫中去,不然數據庫中沒有寫入進去,在鏈接數據庫上添加autocommit=True參數能夠自動提交,此行就不須要了
cur.execute('select * from nhy where name="zhangsan";')
print(cur.fetchall()) #獲取數據庫執行的全部結果,獲取的結果是一個二維的元組
print(cur.fetchone()) #只獲取一條結果
print(cur.fetchmany(2)) #指定獲取幾條結果
#若是上面三條都執行,則只有第一條獲取到,第2、三條則獲取不到東西了,遊標相似於文件指針
cur.close() #遊標關閉
conn.close() #鏈接關閉
操做數據庫封裝爲一個函數
def my_db(ip,user,passwd,db,sql,port=3306,charset='utf8'):
conn = pymysql.connect(host=ip,user=user,
password=passwd,db=db,
port=port,charset=charset,autocommit=True)
cur = conn.cursor()
sql=sql.strip()
cur.execute(sql)
sqlstart = sql[:6].lower() #取sql的開頭6位,轉換爲小寫
if sqlstart.startswith('select') or sqlstart.startswith('show'): #判斷是selec或show的語句獲取對應結果
data = cur.fetchall()
else: #加else是爲了下面的return不報錯
data = 'ok'
cur.close()
conn.close()
return data
#傳統的關係型數據庫(mysql\oracle\sql server\sqlite\db2)
#數據存在磁盤上,使用sql語句來操做數據,表與表之間有關係
#非關係型數據庫(nosql)(mongodb\redis)
#數據存儲是key-value鍵值對的形式;
#mogodb數據存在磁盤上的
#redis數據都是存在內存裏面
import redis
r = redis.Redis(host='1.1.1.1',port=6379,password='123456',db=10) #鏈接redis
#增刪改查
r.set('jxz_info','name zhangsan age 18 sex nan',50) #增長數據,第三個參數50是設置過時失效時間,單位是秒
res = r.get('jxz_info') #查詢數據,獲取到的數據前面有個b,表示是bytes二進制數據
print(res) #get一個不到的key,不會報錯,返回的是None,返回None的話則不能再使用decode()進行編碼轉換,不然報錯
print(res.decode()) #編碼,將二進制轉換成字符串
r.delete('aaaa_info') #指定一個key刪除,刪除一個不存在的key不會報錯
print(r.keys()) #獲取到全部的key
print(r.keys('*info')) #獲取以info結尾的key
print(r.exists('jxz_info')) #判斷這個key是否存在
print(r.flushdb()) #清空當前數據庫裏面全部的key
在redis客戶端軟件上直接執行非關係型數據操做方法:
#hash類型的key增刪改查,哈希類型理解爲字典嵌套一個字典
r.hset('session_jxz','aaa','123456') #增長一個hash類型的key,第一個是外面的key,第二個是裏面的key,第三個是value
r.hset('session_jxz','bbb','000000')
#修改也是hset
r.hdel('session_jxz','bbb') #刪除指定的小key
r.hdel('session_jxz') #直接刪除大key
r.hget('session_jxz','aaa') #查詢指定小key裏的數據
r.hgetall('session_jxz') #查詢指定大key下的全部數據
dic={}
res = r.hgetall('session_jxz')
#將hash key中獲取的數據從bytes類型轉換爲字符串類型
#方法一:
for k,v in res.items():
dic[k.decode()] = v.decode() #把獲取到的key和value的byte類型轉換爲字符串類型;
print(dic)
#方法二:
for k,v in res.items():
res[k.decode] = res.pop(k).decode() #能節省內存,由於它把原來的key刪掉了
print(res)
r.expire('session_jxz',60) #指定key的失效時間
print(r.ttl('session_jxz'))
print(r.type('session_jxz')) #查看key的類型
import hashlib
s = '123456'
m = hashlib.md5(s.encode()) #字符串轉換成bytes類型使用encode()方法便可,要將字符串加密,必須傳一個bytes類型的數據
print(m.hexdigest()) #獲取到md5加密的結果,md5算法加密是不可逆的
#全部同樣的字符串,md5以後的結果都是同樣的
#撞庫,網上的md5在線解密是用撞庫來實現的,是優先將字符串加密的數據與加密前的值放到一個數據庫中,在線查詢是直接到數據庫中去查詢
n = hashlib.sha224(s.encode())
print(n.hexdigest())
def mymd5(s):
str(s)
m = hashlib.md5(s.encode())
print(m.hexdigest())
return m
import xlwt
book = xlwt.Workbook() #建立excel
sheet = book.add_sheet('stu_info') #加一個sheet頁
sheet.write(0,0,'學生編號') #指定行列,第一個表示行,第二個表示列
sheet.write(0,1,'學生姓名')
sheet.write(0,2,'學生成績')
sheet.write(1,0,'1')
sheet.write(1,1,'張三')
sheet.write(1,2,'98')
book.save('stu.xls') #必定要用xls保存