一.安裝第三方模塊
兩種方式:
1、pip install xxx
pip問題:
1、提示沒有pip命令的,
把python的安裝目錄、安裝目錄下面的scripts目錄加入到環境變量里面
2、Unknown or unsupported command 'install'
1、先執行 where pip
2、找到不是python目錄下叫pip的文件,改成其他的名字就ok了
3、電腦里面裝了多個python
python2 python3
1、先去python2安裝目錄里面把python.exe的名字改成python2.exe
2、再把python3安裝目錄里面的python.exe名字改成python3.exe
python2 -m pip install xxx
python3 -m pip install xxx
2、手動安裝
1、.whl
pip install /Users/nhy/Downloads/PyMySQL-0.9.2-py2.py3-none-any.whl
2、.tar.gz
先解壓
然后在命令行里面進入到這個目錄下
python setup.py install
二.數據庫操作
取一條數據fetchone:
1 import pymysql 2 conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test',charset='utf8') 3 cur = conn.cursor() #建立游標 4 sql='select * from app_myuser;' 5 cur.execute(sql)#只是執行sql,並不會返回數據 6 #print(cur.fetchall()) #獲取到所有返回的數據 7 print(cur.fetchone()) #只取一條數據 8 cur.close() 9 conn.close()
(1122, 'liuyana03', '123658', 1)
取所有數據fetchall:
import pymysql conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test')
cur = conn.cursor() #建立游標 sql='select * from app_myuser;' cur.execute(sql)#只是執行sql,並不會返回數據 print(cur.fetchall()) #獲取到所有返回的數據 #print(cur.fetchone()) #只取一條數據 cur.close() conn.close()
((1122, 'liuyana03', '123658', 1), (1123, 'wpp', '1234567', 1), (1128, 'wangcan', 'hhhh', 1), (1129, 'wangcan1', 'hhhh', 1), (1185, 'wangliwen', '123456', 1), (1186, 'wangzhihua', '123456', 1), (1187, 'zyb123', '456789', 1), (1188, 'nhy1233', '456789', 1), (1190, 'aaaa', '1234566', 1), (1191, 'zzf', '123456', 1), (1194, 'yulin123', '411111156789', 1), (1195, 'cui123', '000789', 1), (1197, 'nhy001', '456789', 1), (1198, 'liuhn', '123123', 1), (1200, 'zyq123', '123456', 1), (1201, 'hushaoyan', '123456', 1), (1202, 'qml', '456789', 1), (1203, 'qiuqiu', '456789', 1), (1204, 'yj123', '456789', 1), (1208, 'est', '234234', 1), (1211, 'dyx123', '456789', 1), (1214, 'liucuiyun', '123456', 1), (34353, 'lyc', '123456', 0), (34360, '猜我是誰', '1234566', 1), (34363, 'tanailing', '456789', 2), (34365, 'zjh', '456789', 1), (34366, 'gzo', '123456', 1), (34368, 'liuchang', '123456', 1), (34370, 'yxj999', '123456', 1), (34371, 'tianlixin12', '400789', 1), (34372, 'yh', '456789', 1), (34374, 'dapengpeng', '456789', 1), (34375, 'n1233333', '123456', 1), (34377, 'qqcttp', '123456', 1), (34379, 'liuchang2', '123456', 1), (34382, 'zhang333', '34343', 3), (34384, 'zhizhi123', '456789', 1), (34385, 'tian123', '400789', 1), (34388, 'yaya', '123098', 1), (34389, 'tianrrrrr12', '400789', 1), (34391, 'nhy123', '456789', 1), (34392, 'lyc123', '123456', 0), (34393, 'qml001', '456789', 1), (34399, 'zhangjuanho', '456789', 1), (34406, 'm1233333', '123456', 1), (34415, 'gzo1', '123456', 1), (34416, 'xuxinya', '123456', 1), (34417, 'yh1', '456789', 1), (34420, 'est1', '234234', 1), (34421, 'yy1233333', '123456', 1), (34424, 'zhangjh', '456789', 2), (34425, 'zhangjuh', '456789', 2), (34426, 'est11', '234234', 1), (34430, 'nhuy1233', '456789', 1), (34433, 'yy1233', '123456', 1), (34435, 'test2', '234234', 1), (34442, 'zyb1234', '456789', 1), (34444, 'zyb12345', '456789', 1), (34446, 'yh11', '456789', 1), (34465, 'zhizhi456', '456789', 1), (34479, '你是豬', '123456', 1), (34484, 'xuxinya1', '123456', 1), (34485, 'cuimeiping', '100789', 1), (34486, 'nhuey1233', '456789', 1), (34488, 'y951233', '123456', 1), (34490, 'hgh', '4567890', 1), (34491, 'zzf1', '123456', 1), (34493, 'yjj123', '456789', 1), (34494, 'lyy', '12333', 1), (34495, 'yanran', '456789', 1), (34496, 'cuimeiping1', '100789', 1), (34497, 'nhy002', '456789', 1), (34500, 'qml002', '456789', 1), (34501, 'fff', '456789', 1), (34502, 'lyy1', '12333', 1), (34503, 'ffff', '456789', 1), (34504, 'lyy2', '12333', 1), (34507, 'chunji111', '456789', 10), (34513, 'nhue1233', '456789', 1), (34515, 'lyy5', '12333', 1), (34519, 'zyh1', '123456', 1), (34522, 'test202', '234234', 1), (34524, 'test203', '234234', 1), (34525, 'nhy1230', '456789', 1), (34527, 'zyh11', '123456', 1), (34531, 'khy7744', '1234456', 3), (34534, 'liucuiyun01', '123456', 1), (34535, 'liu0001', '123456', 1), (34536, 'liu0002', '123456', 1), (34540, 'liucuiyun03', 'e10adc3949ba59abbe56e057f20f883e', None), (34542, 'liucuiyun05', 'e10adc3949ba59abbe56e057f20f883e', None), (34543, 'liucuiyun06', 'c33367701511b4f6020ec61ded352059', 1), (34544, 'zyb000', '123456', 1), (34545, 'zyb888', '827ccb0eea8a706c4c34a16891f84e7b', 1), (34546, 'zyq111', 'e10adc3949ba59abbe56e057f20f883e', 1), (34547, 'zyb999', 'e10adc3949ba59abbe56e057f20f883e', 1), (34549, 'qml003', '456789', 1), (34550, 'zhy999', '123456', None), (34551, 'zyh998', '123456', None), (34553, 'zyh997', '123456', None), (34554, 'zyh996', '123456', None), (34555, 'zyh990', '123456', None), (34556, 'wtt', 'e10adc3949ba59abbe56e057f20f883e', 1), (34557, 'wttwtt123', '7d37e3cbff8e2f569ee74d9393c5a4a1', 1), (34568, 'wtt12888', 'f7177163c833dff4b38fc8d2872f1ec6', 1), (34569, 'hsy123', '456789', 1), (34584, 'test666', '123', 1), (34597, 'rrr', '28dd2c7955ce926456240b2ff0100bde', 1), (34598, 'wtt8888', '2a38a4a9316c49e5a833517c45d31070', 1), (34599, 'a', '7c5398c9fd9b0376af683ce0681f93b3', 1), (34601, 'b', 'c1bcf3bfb0c27f357d8f9385dc2ee38b', 1), (34602, 'cc', 'cb39f3d2456ef872af118512d1a0c2f5', 1), (34603, 'qmltest', 'qmltest', 1), (34605, 'jjj', 'jjj', 1), (34611, 'qmlqmlqml', 'qmlqmlqml', 1), (34615, 'hr001', 'c70978b200e6f7507cd55e341f022550', 1), (34618, 'qml123', 'qml123', 1), (34619, 'username', 'news_pwd', 1), (34620, 'rfdsfsdf', 'fsdfsdf', 1), (34622, 'hr002', '7e8feb2276322ecddd4423b649dfd4d9', 1), (34623, 'hzh', 'fc49d07911be544a10e819426734d03a', 1), (34624, 'hr003', '7e8feb2276322ecddd4423b649dfd4d9', 1), (34625, 'zyh666', '123456', None), (34626, 'hr004', '7e8feb2276322ecddd4423b649dfd4d9', 1), (34627, 'test666gaga', '123', 1), (34632, 'tessssstt', 'abe45d28281cfa2a4201c9b90a143095', 1), (34633, 'tessstt', 'abe45d28281cfa2a4201c9b90a143095', 1), (34634, 'tess1', 'ba88c155ba898fc8b5099893036ef205', 1), (34636, 'ytest', '8b634156edde77e407764d5166e34d20', 1), (34637, 'testss', '8b634156edde77e407764d5166e34d20', 1), (34638, 'tess12', '8b634156edde77e407764d5166e34d20', 1), (34639, 'liuhn111', '1eb2890183fd29a6bb55282cc0b0c853', 1), (34640, 'hate', '8b634156edde77e407764d5166e34d20', 1), (34649, 'liuhn001', '1eb2890183fd29a6bb55282cc0b0c853', 1), (34651, 'hushaoyan1', '123456', 1), (34652, 'liuhn002', '1eb2890183fd29a6bb55282cc0b0c853', 1), (34653, 'zyh889', '251b57454f0c600aa20a51ab03756845', None), (34654, 'zyh678', '251b57454f0c600aa20a51ab03756845', None), (34655, 'nhy1123', '456789', 1), (34657, 'nhy11263', '456789', 1), (34658, 'hdl1', '456789', 1), (34660, 'hdl2', '456789', 1), (34661, 'hdl3', '456789', 1), (34663, 'hdl4', '456789', 1), (34664, 'zhangyq', 'e10adc3949ba59abbe56e057f20f883e', 1), (34666, '111', '111', None), (34678, '1111', '111', None), (34679, '5555', '11', None), (34680, 'chun111', '456789', 10), (34682, 'chu3111', '456789', 10), (34683, 'ch2u3111', '456789', 10), (34684, 'zha11san', '3424i', 1), (34686, '2324213', '321', 1), (34687, 'sf4sdf', '232124', 1), (34688, 'dasd45', '453', 1), (34689, 'sdfsdf', '45345', 1), (34690, 'TPRERE', 'cd294ce73f97a8f66b8a6fae31d32dfe', 1), (34691, 'xiaohe123', '123456', 1), (34692, 'hushaoyan2', 'e10adc3949ba59abbe56e057f20f883e', 1), (34693, 'ghghgh', '<md5 HASH object @ 0x00000000029', 1), (34694, 'dasdas34', 'b3ddbc502e307665f346cbd6e52cc10d', 1), (34695, 'shan02', 'e10adc3949ba59abbe56e057f20f883e', 1), (34696, 'zhuxi001', '123123', 1), (34698, 'zhuxi002', '123123', 1), (34699, 'zhuxi003', '123123', 1), (34700, 'zhuxi004', '123123', 1), (34708, '1', '123123', 0), (34711, 'test1', '8b634156edde77e407764d5166e34d20', 1), (34712, 'yutest', 'abe45d28281cfa2a4201c9b90a143095', 1), (34714, 'hsy001', 'e10adc3949ba59abbe56e057f20f883e', 1), (34718, 'hhh', '4567890', 1), (34721, 'kkk', '4567890', 1), (34726, 'sss', '4567890', 1), (34729, 'kk', 'dead7d2efb1e8e354edf3723c1e700f7', 1), (34734, "'", '178a1dfdbd13268d265fee2c9925e564', 1), (34735, "''", 'dead7d2efb1e8e354edf3723c1e700f7', 1), (34737, '小哥哥', 'f594768cb81bf3193fc87235ffa689ab', 1), (34738, 'fffg', '476be781e259f92e9d7c376c787df5e9', 1), (34739, '1122', '52c69e3a57331081823331c4e69d3f2e', 1), (34740, 'hhhh', '52c69e3a57331081823331c4e69d3f2e', 1))
以上的數據均是以元組形式返回,如果需要以字典形式返回,需要在建立游標的時候指定關鍵字,當有多個返回的時候,是將所有的dict放在一個list里返回:
1 import pymysql 2 conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test')
3 cur = conn.cursor(pymysql.cursors.DictCursor) #建立游標,結果以dict形式返回
4 sql='select * from app_myuser;'
5 cur.execute(sql)#只是執行sql,並不會返回數據
6 #print(cur.fetchall()) #獲取到所有返回的數據
7 print(cur.fetchone()) #只取一條數據
8 cur.close()
9 conn.close()
{'id': 1122, 'username': 'liuyana03', 'passwd': '123658', 'is_admin': 1}
fetchone與fetchall不能同時使用,返回的結果在一個結果集中,cur.execute(sql)結果有10條,當fetone先執行,取走一條,fetchall再去取,只能取到剩下的9條,而非10條;反之,fetchall先去取,則10條全部取走,fetchone再去取則一條也取不到,獲得None
數據庫插入后,雖然執行了但實際數據不會提交到數據庫,必須執行commit操作:
1 import pymysql 2 conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test')
3 cur = conn.cursor(pymysql.cursors.DictCursor) #建立游標
4 sql='insert into app_myuser (username,passwd,is_admin) values ("nhy123","456789",1);'
5 cur.execute(sql)#只是執行sql,並不會返回數據
6 conn.commit()
7 cur.close()
8 conn.close()
如果不想每次都手動提交,可以再connect的時候設置autocommit,以上代碼等同於:
1 import pymysql 2 conn = pymysql.connect(host='192.168.1.2',user='test',password='123456',port=3306,db='test',autocommit=True)
3 cur = conn.cursor(pymysql.cursors.DictCursor) #建立游標
4 sql='insert into app_myuser (username,passwd,is_admin) values ("nhy123","456789",1);
5 cur.execute(sql)#只是執行sql,並不會返回數據
6 cur.close()
7 conn.close()
三.加密
1 import hashlib #python3 2 #import md5 #python2 3 s = 'admin123wefsdfsdddddddddd345345dsfdsfcsafsadfds' 4 #bytes 5 m = hashlib.md5(s.encode()) 6 print(m.hexdigest()) 7 m = hashlib.sha224(s.encode()) 8 print(m.hexdigest()) 9 m = hashlib.sha256(s.encode()) 10 print(m.hexdigest()) 11 m = hashlib.sha512(s.encode()) 12 print(m.hexdigest())
b38bbea537ed1405e53e86c274337573
4b22dc6e3ae830d943270da0b82a12836c78c6e9f4f53c2ed229e07a
362b4b094e1cf27ccc3943e93be48c2097060194c5c679a018908fe2bf7c65a7
e56e13d0ceea515a50beed24b910e52b8363e41a85a2f4485c7ea49c1d79cf2f37e7167aa635478c05ab4e06a5262d766f30eceaf5f098bf8ae3ee881bcadd7a
需要導入hashlib包,字符串不能直接加密,加密之前需要進行encode編碼成byte后才能進行加密,並且加密之后是無法逆操作解密的。
為了增加暴力破解的難度,還需要加鹽操作:
1 def my_md5(s,salt=''): 2 s = s+salt 3 news = str(s).encode() 4 m = hashlib.md5(news) 5 return m.hexdigest() 6 7 print(my_md5("degkjhsd"))
9663eb1ee05a1cf7f89bb1b47c0ebbeb
四.遞歸
遞歸通俗的解釋就是自己調自己,Python有遞歸次數限制,默認最大次數為1000
遞歸比較方便的一種用法是遍歷文件目錄查找某個指定文件,使用os.walk:
1 import os 2 def find_movie(keyWord,path=r"C:/Users/Administrator/Desktop/"): 3 for cur_path, cur_dirs, cur_files in os.walk(path): 4 for file in cur_files: 5 if file.endswith(keyWord): 6 print(cur_path) 7 find_movie('.html')
C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/ C:/Users/Administrator/Desktop/print C:/Users/Administrator/Desktop/print C:/Users/Administrator/Desktop/print2\print2 C:/Users/Administrator/Desktop/print2\print2 C:/Users/Administrator/Desktop/print2\print2 C:/Users/Administrator/Desktop/print2\print2 C:/Users/Administrator/Desktop/print3
五.內置函數
1.math模塊:
1 import math 2 res1 = max([1,2,3,4]) 3 res2 = sum(range(1,101)) 4 print(res1) 5 print(res2) 6 print(chr(66)) #把數字轉成對應ascii碼表里面對應的值 7 print(ord('A')) #把字母轉成對應ascii碼表里面對應的數字 8 print(round(80.23556, 2))#返回浮點數的四舍五入值
4 5050 B 65
80.24
2.dir:查看某個對象里有哪些方法
import random print(dir(list)) print(dir(random))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
3.bool:進行布爾轉換,結果為True或False
1 print(bool(None)) #轉布爾類型的 True False 2 print(bool('')) #轉布爾類型的 True False 3 print(bool([])) #轉布爾類型的 True False 4 print(bool({})) #轉布爾類型的 True False 5 print(bool(())) #轉布爾類型的 True False 6 print(bool(0)) #轉布爾類型的 True False 7 print(bool(123)) #轉布爾類型的 True False 8 print(bool([1,3,4])) #轉布爾類型的 True False 9 print(bool([1.5])) #轉布爾類型的 True False 10 #非空即真,非0即真
False
False
False
False
False
False
True
True
True
4.sort 與 sorted :
- sort 是應用在 list 上的方法,屬於列表的成員方法,將原list排序,返回None;sorted是一個內建函數,可以對所有可迭代的對象進行排序操作。
- list 的 sort 方法返回的是對已經存在的列表進行操作,而內建函數 sorted 方法返回的是一個新的 list,而不是在原來的基礎上進行的操作。
- sort使用方法為ls.sort(),而sorted使用方法為sorted(ls)
1 s=[3,4,5,6,1,2,8] 2 s.sort() 3 print(s) 4 s='356128' 5 print(sorted(s,reverse=True))#排序reverse=True降序,False為升序(默認) 6 print(list(reversed(s)))#反轉
[1, 2, 3, 4, 5, 6, 8] ['8', '6', '5', '3', '2', '1'] ['8', '2', '1', '6', '5', '3']
5.zip:
用於將可迭代的對象作為參數,將對象中對應的元素打包成一個個元組,然后返回由這些元組組成的對象。zip() 返回的是一個對象。如需展示列表,需手動 list() 轉換。如果各個迭代器的元素個數不一致,則返回列表長度與最短的對象相同。
1 name = ['nhy','lyl','qlm'] 2 money = [50,100,1000,50,50] 3 print(zip(name,money)) 4 print(list(zip(name,money)))
<zip object at 0x00000000025EB548> [('nhy', 50), ('lyl', 100), ('qlm', 1000)]
通過*可以解壓:
1 a = [1,2,3] #此處可迭代對象為列表 2 b = [4,5,6] 3 4 v = zip(a, b) # 壓縮 5 print(list(v)) 6 7 w = zip(*zip(a, b)) # 解壓 8 print(list(w))
[(1, 4), (2, 5), (3, 6)]
[(1, 2, 3), (4, 5, 6)]
1 #v1,v2,v3可是是任何可迭代對象,如:字符串、列表、元祖、字典 2 v1 = {1:11,2:22} #此處可迭代對象為字典 3 v2 = {3:33,4:44} 4 v3 = {5:55,6:66} 5 6 v = zip(v1,v2,v3) #壓縮 只講key壓縮 7 print(list(v)) 8 9 w = zip(*zip(v1,v2,v3)) #解壓 10 print(list(w))
[(1, 3, 5), (2, 4, 6)]
[(1, 2), (3, 4), (5, 6)]
6.filter與map:
filter函數用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表對象,需要手動list將其轉換。
該接收兩個參數,第一個為函數,第二個為序列,序列的每個元素作為參數傳遞給函數進行判,然后返回 True 或 False,最后將返回 True 的元素放到新列表中。
1 def is_odd(n): 2 return n % 2 == 1 3 4 newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 5 print(newlist) 6 print(list(newlist))
<filter object at 0x000000000259D390>
[1, 3, 5, 7, 9]
map和filter類似,不同的是他不會幫你過濾,只是依次將序列作用於函數上,注意比較上述代碼
1 def is_odd(n): 2 return n % 2 == 1 3 4 newlist = map(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 5 print(newlist) 6 print(list(newlist))
<map object at 0x000000000260D390>
[True, False, True, False, True, False, True, False, True, False]
7.eval:
eval函數就是實現list、dict、tuple與str之間的轉化
str函數把list,dict,tuple轉為為字符串
1 # 字符串轉換成列表 2 a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" 3 print(type(a)) 4 b = eval(a) 5 print(b) 6 # 字符串轉換成字典 7 a = "{1: 'a', 2: 'b'}" 8 print(type(a)) 9 b = eval(a) 10 print(type(b)) 11 print(b) 12 # 字符串轉換成元組 13 a = "([1,2], [3,4], [5,6], [7,8], (9,0))" 14 print(type(a)) 15 b=eval(a) 16 print(type(b)) 17 print(b)
<class 'str'> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] <class 'str'> <class 'dict'> {1: 'a', 2: 'b'} <class 'str'> <class 'tuple'> ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
8.exec:執行一段“”“”直接的python代碼(前提是這段代碼是正確的)
1 s2 =''' 2 import os 3 print(os.getcwd()) 4 for i in range(5): 5 print(i) 6 ''' 7 8 exec(s2) #執行一些簡單的python代碼,運算、定義變量
0
1
2
3
4
9.匿名函數:
即臨時定義的一個只有函數體沒有函數名的函數,改函數因為沒有函數名則不可被調用,一次性有效:
語法 lambda 參數列表: 表達式 (冒號前面是入參,冒號后面是返回值)
res = map(lambda num:str(num).zfill(2),range(1,34)) print(list(res))
匿名函數中:num是入參,str(num).zfill(2)是函數體;由lambda生成函數后,用range(1,34)組合成一個map函數
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33']