11,846
社区成员




最近在测试的时候,比如导入这个功能,想要测试上传大数据量文件是否好用,有两个点需要解决:一是怎么生成大量的excel文件,二是如何保证生成的文件格式与项目要求的格式一致。
有人可能想到从晚上下载大容量的文件,但是格式这一块无法保证。
还有一种笨方法可以复制粘贴,粘贴几十条数据还可以,大容量这块无法保证。
这时候脚本的作用就体现了,利用程序的思想解决问题,其实就是excel写入数据的过程。接下来看看~
实现效果:
可以按照实际的场景想象一下,然后用机器的语言实现。
我们需要创建一个excel文件,然后写好标题,之后按照标题填写内容,写好之后保存。
拆分代码功能区:
1.创建一个excel对象->创建sheet
2.添加字段、行、列、名称
3.写入编号字段数据
4.保存
上面稍微复杂一点的时步骤3,接下来我们按照功能点一行一行填写代码。
按功能点填充代码:
1.导入相关包
import xlwt #导入xlwt 函数,专门操作excel写文件的函数
import random #导入随机函数
2.创建工作簿对象->创建sheet
#创建工作簿对象
book=xlwt.workbook(encoding='utf-8')
#创建sheet
sheet=book.add_sheet('test',cell_overwrite_ok=True)
创建工作使用workbook()函数;
创建表使用book.add_sheet()函数,其中一个参数为名称,第二个参数为是否支持重写。
3.添加标题内容:字段、行、列、名称
sheet.write(0, 0, '部门ID')
sheet.write(0, 1, '部门名称')
sheet.write(0, 2, '部门顺序')
sheet.write(0, 3, '负责人')
sheet.write(0, 4, '联系电话')
sheet.write(0, 5, '邮箱')
sheet.write(0, 6, '状态')
sheet.write(0, 7, '上级部门名称')
sheet.write(0, 8, '部门编号')
sheet.write(0, 9, '部门简称')
使用sheet.write(行,列,内容)函数进行填充,一般标题都是第0行。
4.写入编号字段数据
for i in range(10):
sheet.write(i+1,0,300+id)
sheet.write(i+1,1,random.choice([测试],[开发],[产品]))
sheet.write(i+1,7,'齐大山铁矿')
sheet.write(id + 1, 6, "正常")
sheet.write(id + 1, 8, random.randint(1, 100))
for循环,逐行写入内容;
random.choice()函数实现随机选择字符串;
random.randint(1,100)实现1,100随机数。
保存
book.save(r'.\部门信息.xlsx')
实现代码:
# -*- coding: utf-8 -*-
# @Time : 2018/12/6 17:10
# @Author : taozi
# @Disc: : 生成百万条条Excel数据
# @File : 1000data.py
# @Software: PyCharm
import xlrd ,xlwt
import random
"""创建一个excel对象"""
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
"""写入编号字段数据"""
def write():
sheet.write(0, 0, '部门ID')
sheet.write(0, 1, '部门名称')
sheet.write(0, 2, '部门顺序')
sheet.write(0, 3, '负责人')
sheet.write(0, 4, '联系电话')
sheet.write(0, 5, '邮箱')
sheet.write(0, 6, '状态')
sheet.write(0, 7, '上级部门名称')
sheet.write(0, 8, '部门编号')
sheet.write(0, 9, '部门简称')
for id in range(60000):
sheet.write(id + 1, 0, id+300)
sheet.write(id + 1, 1, random.choice(['测试', '开发', '生产']))
sheet.write(id + 1, 2, random.randint(1, 100))
sheet.write(id + 1, 6, "正常")
sheet.write(id + 1, 7, "1111")
sheet.write(id + 1, 8, random.randint(1, 100))
"""创建sheet并调用write函数写入数据"""
for j in range(3):
sheet = book.add_sheet('test'+str(j),cell_overwrite_ok=True)
write()
book.save(r'.\部门信息.xls')
细心的可以发现,部门id是唯一的字段,我们这样添加内容唯一。那么如何生成唯一的部门id值呢?
我们都知道random可以随机生成数,那么这个数怎么保证唯一呢?其中有一个函数sample就可以实现。
把可选的字符串定义一个变量,然后从中选取x位字符。
代码如下:
strings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
random_str = random.sample(strings, 6)
最终代码:
# -*- coding: utf-8 -*-
# @Time : 2018/12/6 17:10
# @Author : taozi
# @Disc: : 生成百万条条Excel数据
# @File : 1000data.py
# @Software: PyCharm
import xlrd ,xlwt
import random
"""创建一个excel对象"""
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
"""写入编号字段数据"""
def write():
sheet.write(0, 0, '部门ID')
sheet.write(0, 1, '部门名称')
sheet.write(0, 2, '部门顺序')
sheet.write(0, 3, '负责人')
sheet.write(0, 4, '联系电话')
sheet.write(0, 5, '邮箱')
sheet.write(0, 6, '状态')
sheet.write(0, 7, '上级部门名称')
sheet.write(0, 8, '部门编号')
sheet.write(0, 9, '部门简称')
for id in range(60000):
strings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
random_str = random.sample(strings, 6)
sheet.write(id + 1, 0, random_str)
sheet.write(id + 1, 1, random.choice(['测试', '开发', '生产']))
sheet.write(id + 1, 2, random.randint(1, 100))
sheet.write(id + 1, 6, "正常")
sheet.write(id + 1, 7, "1111")
sheet.write(id + 1, 8, random.randint(1, 100))
"""创建sheet并调用write函数写入数据"""
for j in range(3):
sheet = book.add_sheet('test'+str(j),cell_overwrite_ok=True)
write()
book.save(r'.\部门信息.xls')
实现效果:
回顾一下操作excel写入文件的步骤:创建工作簿->创建sheet->写入字段名称->写入内容,分别用到了Workbook(),add_sheet(), write()函数。
同时过程中使用for循环创建表及函数的调用,再有就是利用random.sample()函数生成唯一字段。
好了,今天就到这儿吧,小伙伴们点赞、收藏、评论,一键三连走起呀,我是测试君,我们下期见~~
2020软件测试工程师面试题汇总(内含答案)-看完BATJ面试官对你竖起大拇指!
面试经:一线城市搬砖!又面软件测试岗,5000就知足了...
转行面试,跳槽面试,软件测试人员都必须知道的这几种面试技巧!