一、python自有模塊正則
1 import re 2 3 # re.match只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數返回None 4 print(re.match("www","wwwwccc").group()) #在起始位置匹配 5 print(re.match("www","wcccwww")) #不在起始位置匹配,返回None 6 7 # re.search掃描整個字符串並返回第一個成功的匹配 8 print(re.search("haha","woshihahanishishui").group()) 9 10 # re.findall從左到右掃描字符串,按順序返回匹配,如果無匹配結果則返回空列表 11 print(re.findall("\d","one1two2three3four4")) 12 print(re.findall("\d","onetwothreefour")) 13 14 # sub用於替換字符串中的匹配項 15 print(re.sub("g..t","good","goot geet up")) 16 # split返回切割后的列表 17 print(re.split("\+","123+456*789+abcd"))
group及groups區別:
re.compile將正則字符串編譯成正則表達式對象:
1 # ['AD123453', 'AC342123', 'AR343212'] 2 # [A-Z]{2}\d{6} 3 4 # ^(13|14|15|17|18)\d{9} 5 # ^1[34578]\d{9}
推薦一個正則表達式的小工具:https://pan.baidu.com/s/1EXrSF5pS8hJ7050BOOtL9w
二、python第三方模塊操作MySQL
連接數據庫:
1 import mysql.connector 2 config = { 3 "host":"127.0.0.1", 4 "user":"root", 5 "passwd":"vertrigo", 6 "port":3306, 7 "db":"test", 8 "charset":"utf8",
"buffered"='True'
9 } 10 11 try: 12 db = mysql.connector.connect(**config) 13 except mysql.connector.Error as e: 14 print("連接數據庫失敗!",str(e))
插入數據:
1 2 cursor = db.cursor(buffered=True) #buffered=True會把結果集保存到本地並一次性返回,這樣可以提高性能 3 try: 4 #第一種:直接字符串插入方式 5 # sql_insert1="insert into student (name, age) values ('xiao', 27)" 6 # cursor.execute(sql_insert1) 7 8 #第二種:元組連接插入方式 9 sql_insert2="insert into student (name, age) values (%s, %s)" 10 #此處的%s為占位符,而不是格式化字符串,所以age用%s 11 # data=('xiaoqiang',18) 12 # cursor.execute(sql_insert2,data) 13 data = [("xiao",20), 14 ("xian",25), 15 ("rourou",27), 16 ("juju",28)] 17 cursor.executemany(sql_insert2,data) 18 19 #如果表引擎為Innodb,執行完成后需執行commit進行事務提交 20 db.commit() 21 #cursor.execute('commit') 22 except mysql.connector.Error as e: 23 print('插入失敗!', str(e)) 24 finally: 25 cursor.close() 26 db.close()
刪除數據:
1 cursor = db.cursor(buffered=True) 2 try: 3 sql_del = "delete from student where name=%s and age=%s" 4 data_del = [ 5 ("cui",28), 6 ("hao",27)] 7 cursor.executemany(sql_del,data_del) 8 db.commit() 9 except mysql.connector.Error as e: 10 print("刪除數據失敗!",str(e)) 11 finally: 12 cursor.close() 13 db.close()
修改數據:
1 cursor = db.cursor(buffered=True) 2 try: 3 sql_update = "update student set age = 28 where name='kai'" 4 cursor.execute(sql_update) 5 db.commit() 6 except mysql.connector.Error as e: 7 print('修改數據失敗',str(e)) 8 finally: 9 cursor.close() 10 db.close()
查詢數據:
1 cursor = db.cursor(buffered=True) 2 try: 3 # sql_select1 = "select * from student where age>%s" 4 # cursor.execute(sql_select1,(1,)) 5 6 sql_select2 = "select * from student where age>%s" 7 cursor.execute(sql_select2,(26,)) 8 datas1 = cursor.fetchall() #如果在后加上[1]代表是取第一條數據 9 10 cursor.execute(sql_select2,(20,)) 11 datas2 = cursor.fetchone()[1] #如果在后加上[1]代表是取第一個字段 12 datas3 = cursor.fetchmany(5) #返回5條數據 13 14 print(datas1) 15 print(datas2) 16 print(datas3) 17 except mysql.connector.Error as e: 18 print("查詢數據失敗!",str(e)) 19 finally: 20 cursor.close() 21 db.close()
三、python第三方模塊操作Excel
注意高能:openpyxl只能操作xlsx文件而不能操作xls文件!所以在創建的時候一定要新建.xlsx格式的Excel!!!
1 import openpyxl 2 #打開文件 3 path_file = "D:/PycharmProjects/open.xlsx" 4 wp = openpyxl.load_workbook(path_file) 5 6 print("獲取所有工作表名:",wp.get_sheet_names()) 7 # sheet = wp.get_sheet_by_name("Sheet1") #獲取指定的工作表sheet 8 # print("獲取指定的工作表名:",sheet.title) 9 sheet2 = wp.get_active_sheet() #獲取活動的工作表sheet,一般是當前打開的sheet頁 10 print("獲取活動的工作表名:",sheet2.title) 11 12 13 # 操作單元格:數據的讀取與寫入 14 # 獲取單元格數據 15 print(sheet2['A1'].value) 16 print(sheet2.cell(row=2,column=1).value) #獲取第二行第一列的值 17 18 # 數據的寫入 19 sheet2.cell(row=4,column=1).value = "工作總結" 20 sheet2['C3']='cs' 21 22 print("最大列數",sheet2.max_column) 23 print("最大行數",sheet2.max_row) 24 25 # wp.save('D:/PycharmProjects/open1.xlsx') #另存為 26 wp.save("open.xlsx") #保存,默認保存在當前目錄下 27 wp.save(path_file) #覆蓋保存
小練習:
1、Python里面match()和search()的區別?
python正則中的match是在字符串的起始位置進行匹配,如果起始位置匹配不成功就會報錯。如下例子:
import re
print(re.match("www","wwccc").group())
python正則中的search是在字符串中匹配第一次匹配成功的字符串並返回。如下例子:
import re
print(re.search("ws","wwcccwss").group())
2、以<div><span>test</span></div>進行匹配
<.*>意思為:匹配以<為開始的所有的字符,遇到回車換行中斷匹配
<.*>結果為:<div><span>test</span></div>
<.*?>意思為:匹配以<為開始的字符串,出現一次就返回
<.*?>結果為:
<div>
<span>
<span>
<div>
3、獲取URL的后綴名
abc = ("http://www.baidu.cn/archive/6688431.html")
print(abc.split('.')[-1])