Python自动化常用模块介绍 python自有模块正则 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import reprint(re.match("blog" , "blog.laosan.xin" ).group()) print(re.match("laosan" , "blog.laosan.xin" )) print(re.search("laosan" , "blog.laosan.xin" ).group()) print(re.findall("\d" , "one1two2three3four4" )) print(re.findall("\d" , "onetwothreefour" )) print(re.sub("g..t" , "good" , "goot geet up" )) print(re.split("\+" , "123+456*789+abcd" ))
re.compile将正则字符串编译成正则表达式对象:
1 2 3 4 5 6 7 # re.compile将正则字符串编译成正则表达式对象: datetime = '2020-11-06 12:30:30' peet = re.compile("\d{2}\d{2}:\d{2}") print("将时分秒提取为表达式对象", peet) result = re.sub(peet, "", datetime) print("将时分秒提取值为", result)
python第三方模块操作MySQL 连接数据库 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 config = { "host" : "127.0.0.1" , "user" : "root" , "passwd" : "root@123" , "port" : 3306 , "db" : "user" , "charset" : "utf8" , "buffered" : "True" } try : db = mysql.connector.connect(**config) except mysql.connector.Error as e: print("连接数据库失败!" , str(e))
插入数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 try : db = mysql.connector.connect(**config) except mysql.connector.Error as e: print("连接数据库失败!" , str(e)) cursor = db.cursor(buffered=True ) try : autocommit = "SELECT @@session.autocommit" print(cursor.execute(autocommit)) sql_insert2 = "insert into user (name, age) values (%s, %s)" data = ('laosan' , random.randint(0 , 100000000 )) cursor.execute(sql_insert2, data) data = [("xiao" , 20 ), ("xian" , 25 ), ("rourou" , 27 ), ("juju" , 28 )] cursor.executemany(sql_insert2, data) db.commit() except mysql.connector.Error as e: print('插入失败!' , str(e)) finally : cursor.close()
删除数据 1 2 3 4 5 6 7 8 9 10 11 cursor = db.cursor(buffered=True ) try : sql_del = "delete from user where name=%s and age=%s" data_del = [("laosan" , "30" )] cursor.executemany(sql_del, data_del) db.commit() except mysql.connector.Error as e: print("删除数据失败!" , str(e)) finally : cursor.close()
修改数据 1 2 3 4 5 6 7 8 9 cursor = db.cursor(buffered=True ) try : sql_update = "update user set age = 28 where name='laosan'" cursor.execute(sql_update) db.commit() except mysql.connector.Error as e: print("修改数据失败!" , str(e)) finally : cursor.close()
查询数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 cursor = db.cursor(buffered=True ) try : sql_select = "select * from user where age > %s" cursor.execute(sql_select,(26 ,)) datas1 = cursor.fetchall() cursor.execute(sql_select,(20 ,)) datas2 = cursor.fetchone()[1 ] datas3 = cursor.fetchmany(5 ) print(datas1) print(datas2) print(datas3) except mysql.connector.Error as e: print("查询数据失败!" ,str(e)) finally : cursor.close()
python第三方模块操作Excel 注意高能:openpyxl只能操作xlsx文件而不能操作xls文件!所以在创建的时候一定要新建.xlsx格式的Excel!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import openpyxlpath_file = "/Users/laosan/Desktop/e/4-1.xlsx" wb = openpyxl.load_workbook(path_file) print("获取所有工作表名" , wb.sheetnames) sheet = wb["Sheet1" ] print("获取指定的工作表名:" , sheet.title) print(sheet['A1' ].value) print(sheet.cell(row=2 , column=1 ).value) sheet.cell(row=4 , column=1 ).value = "修改后的内容" sheet['C3' ] = 'cs' print("最大列数" , sheet.max_column) print("最大行数" , sheet.max_row) wb.save("/Users/laosan/Desktop/e/open.xlsx" ) wb.save(path_file)
参考资料 python自动化–模块操作之re、MySQL、Excel