MD5加密
md5加密是不可逆的
def md5(string,salt=""):
new = str(string) + str(salt)
m = hashlib.md5(new.encode())
return m.hexdigest()
faker模块
import faker
f = 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 openpyxl
book = 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"] #指定的sheet
print(sheet.max_row) #多少行 # 2
print(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 openpyxl
book = openpyxl.load_workbook("info.xlsx")
sheet = book.active
sheet.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开始的 Name
print(sheet.nrows) #总共多少行 2
print(sheet.ncols) #总共多少列 3
print(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 Pinyin
p = Pinyin()
ret = p.get_pinyin("萧萧") # xiao-xiao
ret = p.get_pinyin("萧萧","+") # xiao+xiao
print(ret)
练习:汉字转拼音,并标注重复姓名出现的次数,写入excel中
s="""
廖娟
赵兰英
罗淑英
袁玉华
李艳
李燕
李岩
"""
import openpyxl
name_dict = {}
names = s.split()
book = openpyxl.Workbook()
sheet = book.active
sheet.append(["姓名","账号"])
for name in names:
pinyin = p.get_pinyin(name,'')
if pinyin not in name_dict:
name_dict[pinyin] = 1
else:
name_dict[pinyin]+=1
pinyin_count = name_dict[pinyin]
if pinyin_count!=1:
pinyin = "%s%s" % (pinyin,pinyin_count-1)
sheet.append([name,pinyin])
book.save("用户.xlsx")
练习:给账号加上状态
import openpyxl
book = openpyxl.load_workbook("用户.xlsx")
sheet = book.active
for index,value in enumerate(sheet.values):
if index==0:
continue
if index % 2 ==0:
status = "未添加"
else:
status = "已添加"
sheet.cell(index+1,3).value = status
book.save("new_用户.xlsx")
mysql数据库操作
import pymysql
mysql_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 result
ret = execute_sql("SELECT * FROM `faker_user`;",False,2)
print(ret)
# {'uid': 1, 'username': '阚淑华', 'password': '5f2CmolKhV', 'address': '北京市丹丹县高港胡街H座 164242', 'email': 'taotang@example.com'}