MD5加密
md5加密是不可逆的
def md5(string,salt=""):new = str(string) + str(salt)m = hashlib.md5(new.encode())return m.hexdigest()
faker模块
import fakerf = faker.Faker(locale='zh_CN')print(f.name()) #随机姓名print(f.address()) #随机街道print(f.city()) #随城市print(f.chrome()) #随机浏览器print(f.date_time()) #随机时间print(f.user_name()) #用户名print(f.street_address()) #街道print(f.phone_number()) #手机号print(f.credit_card_full()) #全的信用卡信息print(f.credit_card_number()) #信用卡print(f.free_email()) #邮箱print(f.ipv4()) #ip地址print(f.ipv6()) #ip地址print(f.name_female()) #女名字print(f.name_male()) #男名字print(f.ssn()) #身份证号
Excell操作
1.openpyxl 模块
写入操作:
import openpyxlbook = openpyxl.Workbook() # 实例化# sheet = book.create_sheet("sheet1")#创建一个新的sheet1页sheet = book.active #获取默认的sheet页sheet.append(["姓名","性别","身高"])#写一整行sheet.append(["Sara","Female",160])sheet.cell(1,1,"Name")#指定写入某个单元格book.save("info.xlsx")

读取excell文件内容
book = openpyxl.load_workbook("info.xlsx")sheet = book.active# sheet = book["Sheet"] #指定的sheetprint(sheet.max_row) #多少行 # 2print(sheet.max_column) #多少列 # 3# 获取所有行的数值print(list(sheet.values))#[('Name', '性别', '身高'), ('Sara', 'Female', 160)]# 获取每行的数值for v in sheet.values:#获取所有行print(v)# ('Name', '性别', '身高')#('Sara', 'Female', 160)#获取每列数据for col in sheet.columns:#H获取所有的列print(col)(<Cell 'Sheet'.A1>, <Cell 'Sheet'.A2>)(<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>)(<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>)# 可以用列表生成式for col in sheet.columns:#H获取所有的列# for r in col:# print(r.value)col = [r.value for r in col]print(col)#['Name', 'Sara']#['性别', 'Female']#['身高', 160]print(sheet.cell(1,1).value)#获取指定单元格的内容
修改操作
import openpyxlbook = openpyxl.load_workbook("info.xlsx")sheet = book.activesheet.cell(1,1).value = "name"sheet.delete_cols(1) #删除指定的列sheet.delete_rows(1) #删除指定的行book.save("new_用户.xlsx")
2.xlrd模块
import xlrd#最多可以读取32000行book = xlrd.open_workbook("info.xlsx")# sheet = book.sheet_by_index(0)sheet = book.sheet_by_name("Sheet")print(sheet.cell(0,0).value) #获取指定单元格的内容,是从0开始的 Nameprint(sheet.nrows) #总共多少行 2print(sheet.ncols) #总共多少列 3print(sheet.row_values(0))#指定获取某一行的数据 ['Name', '性别', '身高']print(sheet.col_values(0))#指定获取某一列的数据 ['Name', 'Sara']for i in range(sheet.nrows):#获取所有行的数据print(sheet.row_values(i))for i in range(sheet.ncols):#获取所有列的数据print(sheet.col_values(i))
xpingying模块
把汉字转换成拼音,第二个参数表示以什么符号连接拼音,默认是”-“
from xpinyin import Pinyinp = Pinyin()ret = p.get_pinyin("萧萧") # xiao-xiaoret = p.get_pinyin("萧萧","+") # xiao+xiaoprint(ret)
练习:汉字转拼音,并标注重复姓名出现的次数,写入excel中
s="""廖娟赵兰英罗淑英袁玉华李艳李燕李岩"""import openpyxlname_dict = {}names = s.split()book = openpyxl.Workbook()sheet = book.activesheet.append(["姓名","账号"])for name in names:pinyin = p.get_pinyin(name,'')if pinyin not in name_dict:name_dict[pinyin] = 1else:name_dict[pinyin]+=1pinyin_count = name_dict[pinyin]if pinyin_count!=1:pinyin = "%s%s" % (pinyin,pinyin_count-1)sheet.append([name,pinyin])book.save("用户.xlsx")

练习:给账号加上状态
import openpyxlbook = openpyxl.load_workbook("用户.xlsx")sheet = book.activefor index,value in enumerate(sheet.values):if index==0:continueif index % 2 ==0:status = "未添加"else:status = "已添加"sheet.cell(index+1,3).value = statusbook.save("new_用户.xlsx")

mysql数据库操作
import pymysqlmysql_info = {"host":"**.**.**.**","user":"root","passwd":"123456","db":"test","port":3306,"charset":"utf8","autocommit":True}def execute_sql(sql,all=True,cur_type=1):#cur_type如果是1,那么返回的是list#如果是2,那么返回的字典connect = pymysql.connect(**mysql_info)cur = connect.cursor() if cur_type == 1 else connect.cursor(pymysql.cursors.DictCursor)cur.execute(sql)if all:result = cur.fetchall()else:result = cur.fetchone()cur.close()connect.close()return resultret = execute_sql("SELECT * FROM `faker_user`;",False,2)print(ret)# {'uid': 1, 'username': '阚淑华', 'password': '5f2CmolKhV', 'address': '北京市丹丹县高港胡街H座 164242', 'email': 'taotang@example.com'}
