1.使用uuid进行唯一编码,写入新文件
file_name = 'case_%s.xls'%(uuid.uuid4().hex)
2.如果文件路径不存在,就创建。
if not os.path.exists(path_store):
os.makedirs(path_store)
3.打开并写入文件
with open(upload_path, 'wb+') as f2:
content = f.read()
f2.write(content)
这里的upload_path指的是具体到文件名称的文件路径,f指前端传入的文件
4.使用Python的xlrd库读取文件内容
data = xlrd.open_workbook(upload_path)
sheet = data.sheet_by_name("sheet1")
5.写入前将每行数据提取
for i in range(0, sheet.nrows):
returnData = sheet.row_values(i)
6.有用到Python的集合,利用集合中没有重复元素这一特性去重。
case_belong_set = set()#定义集合
case_belong_set.add(case_belong)#向集合中传值
case_belong_list = list(case_belong_set)#集合转列表
belong = ''.join(case_belong_list).encode('utf-8')#列表转字符串
7.贴上全部代码仅供参考写法;
import xlrd
import os
from models import *
import re
import uuid
import cases_business
def data_check(f,version_name,path_store, username, admin_right):
#将excel写入新的文件
file_name = 'case_%s.xls'%(uuid.uuid4().hex)
upload_path = os.path.join(path_store, file_name)
if not os.path.exists(path_store):
os.makedirs(path_store)
with open(upload_path, 'wb+') as f2:
content = f.read()
f2.write(content)
data = xlrd.open_workbook(upload_path)
sheet = data.sheet_by_name("sheet1")
case_add_num = 0
modify_name_num = 0
modify_text_num = 0
for i in range(0, sheet.nrows):
returnData = sheet.row_values(i)
module_path_id = returnData[0]
name = returnData[1]
text = returnData[2]
model_id = returnData[3]
case_belong = returnData[4]
if "|" not in text:
return False, u'第%s行用例内容格式错误!'%i, file_name
try:
model_id = int(model_id)
except:
return False,u'第%s行用例拓扑为非数值类型'%i,file_name
if "." not in module_path_id:
return False, u'第%s行用例路径未分级'%i, file_name
if admin_right:
pass
else:
case_belong_set = set()
case_belong_set.add(case_belong)
case_belong_list = list(case_belong_set)
belong = ''.join(case_belong_list).encode('utf-8')
print belong
print username
if len(case_belong_list) == 1:
if belong == username:
pass
else:
return False, u'该用例不属于当前用户,不允许修改他人用例!', file_name
else:
return False, u'不允许多人上传!', file_name
case_code_p = '\d+[A-Za-z\.\d-]+\d+' # 用例编号正则表达式
m = re.search(case_code_p, name)
if m is None:
return False, u'第%s行用例无法解析出用例编号!' % i, file_name
else:
case_code = m.group()
case_code_list = []
case_code_list.append(case_code)
for i in case_code_list:
case_obj = OneCaseScheam.objects(version_name=version_name, case_code=i).first()
if case_obj is not None:
if i in case_obj.case_code:
if name == case_obj.name:
pass
else:
modify_name_num += 1
if text == case_obj.text:
pass
else:
modify_text_num += 1
else:
case_add_num += 1
print case_add_num, modify_name_num, modify_text_num
msg = u'''
有%s条用例新增
'有%s条用例名称被修改
有%s条用例内容被修改
''' % (case_add_num, modify_name_num, modify_text_num)
return True, msg, file_name
def data_import(excel_file_name,version_name):
upload_path = os.path.join(os.getcwd(), 'case_app\static\upload',excel_file_name)
data = xlrd.open_workbook(upload_path)
sheet = data.sheet_by_name("sheet1")
for i in range(sheet.nrows):
returnData = sheet.row_values(i)
module_path_id = returnData[0]
name = returnData[1]
text = returnData[2]
model_id = returnData[3]
case_belong = returnData[4]
case_code_p = '\d+[A-Za-z\.\d-]+\d+' # 用例编号正则表达式
m = re.search(case_code_p, name)
case_code = m.group()
case_code_list = []
case_code_list.append(case_code)
for i in case_code_list:
data_dict = {'version_name': version_name, 'case_text': text,
'module_path_id': module_path_id, 'case_name': name,
'topo_model_id': model_id, 'topo_model_name': None, 'topo_model_content': None}
case_obj = OneCaseScheam.objects(version_name=version_name, case_code=i).first()
if case_obj is not None:
case_id = case_obj.id
data_dict['case_id'] = case_id
cases_business.modify_case_content(data_dict, role='test',belong=case_belong)
else:
cases_business.add_case(data_dict,role='test',belong=case_belong)
return True,None,excel_file_name
if __name__ == '__main__':
from mongoengine import *
connect("T2_DB", host="mongodb://%s:%s" % ('192.168.84.230', 30000))
该代码实现的功能是在自动化平台上实现导入导出的后端代码,其中有涉及到数据的检查以及数据的校对(数据校对在导出时有用到)