读取excel
import xlrd #只能读
book = xlrd.open_workbook('nhy.xls')
print(book.nsheets)#获取到excel里面总共有多少个sheet页
sheet = book.sheet_by_index(0)
#book.sheet_by_name('sheet1')#
# print(sheet.cell(0,0).value) #指定行和列,获取某个单元格里面的内容
# print(sheet.cell(1,0).value)
print(sheet.row_values(0))#获取某一行的数据
print(sheet.row_values(1))#获取某一行的数据
print(sheet.nrows)#这个就是excel里面总共有多少行
print(sheet.col_values(0))#某一列的数据
print(sheet.col_values(1))#某一列的数据
print(sheet.ncols)#总共有多少列
修改excel
import xlrd
from xlutils import copy
book1 = xlrd.open_workbook('nhy.xls')
#打开原来的excel
new_book = copy.copy(book1)
#拷贝一个新的excel
sheet = new_book.get_sheet(0)
#获取第一个sheet页
sheet.write(1,3,'18')
sheet.write(1,1,'wYh')
new_book.save('nhy.xls')
操作mongo数据库
import pymongo
client = pymongo.MongoClient(host='IP地址',port=端口号)
db = client['szp']#选择数据库,如果这个数据库不存的话,会帮你创建
collection = db['stu_info']#选择一个集合,就相当于mysql里面表
# db['stu_info'].insert({'url':'http://www.baidu.com','title':'baidu.com','addr':'西二旗'})
# for d in db['stu_info'].find({'title':'baidu.com'}):
# print(d)
collection.delete_one({'title':'baidu.com'})#如果有多条的话,只会帮你删掉1条
# collection.delete_many({'title':'baidu.com'})#会删除多条
# print(list(collection.find()))
collection.update({'jd':'www.jd.com'},{'jd':'www.jd.com','addr':'亦庄'})
操作mysql,将数据库中的内容写入excel中
import pymysql,xlwt
coon=pymysql.connect(host='IP',user='用户名',password='123456',db='jxz',port=3306,charset='utf8')
#cur=coon.cursor(cursor=pymysql.cursors.DictCursor)#指定游标类型
cur=coon.cursor() #指定游标类型
cur.execute('select * from YUZE_userinfo;')
fileds=[fileds[0] for filed in cur.description]# 获取表头信息
data=list(cur.fetchall())
data.insert(0,fileds) #将表头加入到数据中
book=xlwt.Workbook()
sheet=book.add_sheet('sheet1')
#方法一
line=0
for d in data:
col=0
for col_data in d:
sheet.write(line,col,col_data)
col+=1
line+=1
#方法二
for index,line_data in enumerate(data):
for index2,col_data in enumerate(line_data):
sheet.write(index,index2,col_data)
book.save('user.xls')
cur.close()
sys模块
import sys
command=sys.argv #运行python文件时的参数
if len(command)>1:
excel=command[1]
print(’用例已经运行完成',excel)
else:
print('运行这个python文件时,需要传入一个用例名称’)
导入模块的顺序
1、当前目录下找这个python文件
2、去python的环境变量里面找这个python文件
导入模块的实质
把这个模块的代码,从头到尾执行一次
接口开发
import flask,pymysql
import hashlib
import json
server = flask.Flask(__name__) #把当前这个python文件当做一个服务
def my_db(sql,port=3306,charset='utf8'):
ip, user, passwd, db = 'IP','jxz','123456','jxz'
coon = pymysql.connect(host=ip,user=user,
password=passwd,db=db,
port=port,charset=charset,autocommit=True)
cur = coon.cursor()
sql=sql.strip()
cur.execute(sql)
sql_start = sql[:6].lower()#取sql的开头,转成小写
if sql_start.startswith('select') or sql_start.startswith('show'):
data = cur.fetchall()
else:
data = 'ok'
cur.close()
coon.close()
return data
def my_md5(s):
m = hashlib.md5(s.encode())
return m.hexdigest()
@server.route('/login',methods=['get','post']) #装饰器
def login():
username = flask.request.values.get('username')#是从客户端发过来的数据
pwd = flask.request.values.get('password')
#flask.request.cookies.get('xxx')#获取cookie
#flask.request.headers.get('xxxx')#获取headers
# json_data = flask.request.json.get('username') #这个获取入参是json类型的
if username=='nhy' and pwd=='123456':
res = {'code':1000,'msg':'登录成功','sign':'sdfsdf23vsrqsdf'}
else:
res = {'code':2000,'msg':'账号/密码错误'}
return json.dumps(res,ensure_ascii=False)
@server.route('/register',methods=['post'])#装饰器
def reg():
username = flask.request.values.get("username")
passwd = flask.request.values.get("passwd")
cpasswd = flask.request.values.get("cpasswd")
if username and passwd and cpasswd:
sql1 = 'select * from nhy where name="%s";'%username
sql_res = my_db(sql1)
if sql_res:
res = {'code':2001,'msg':'该用户已经被注册'}
else:
if passwd==cpasswd:#
md5_pwd = my_md5(passwd)
sql2 = 'insert into nhy (name,pwd) value ("%s","%s");'%(username,md5_pwd)
my_db(sql2)
res = {'code':1000,'msg':'注册成功'}
else:
res = {'code':2003,'msg':'两次输入的密码不一致'}
else:
res = {'code':2002,'msg':'必填参数未填,请查看接口文档!'}
return json.dumps(res,ensure_ascii=False)
server.run(port=8989,debug=True)#启动服务