软件研发过程中,我们经常需要各种各样的模拟数据来测试软件,为了让数据更真实地接近用户端数据,需要数据满足一定的规则,同时,数据还可能涉及一些逻辑运算等。
首先,开始动手前,想清楚要开发解决什么问题的工具,本案例:自动生成100条满足条件的学生信息,供某软件功能测试时使用,节省手动处理测试数据的时间。
下面,是详细的开发步骤。
开发步骤
第1步:条件准备
第2步:提示词设置
提示词设计如下:
##
我们需要注意,python程序运行的环境,因为不同版本的python存在兼容性问题,而AI 写的脚本程序并不一定在你的环境下可正确运行。所以,在提示词设计时,要明确出来(此处,我踩坑了,是后面才加上去的)
第3步:AI生成脚本程序
下面是kimi输出的python程序:
import openpyxlimport randomfrom openpyxl.utils import get_column_letter# 定义文件路径file_path = 'D:\\temp\\Classinfo.xlsx'# 打开现有的Excel工作簿和工作表try:wb = openpyxl.load_workbook(file_path)ws = wb.activeexcept FileNotFoundError:print(f"File {file_path} not found.")exit()# 读取第2行样例信息sample_row = 2sample_data = [cell.value for cell in ws[1]]# 定义姓氏列表surnames = ["李", "张", "刘", "王", "黄", "肖", "姚"]# 随机生成学生数据for i in range(1, 101):# 从1到100生成100个学生信息# 学号student_id = f"NS{str(i).zfill(4)}"# 姓名surname = random.choice(surnames)name = f"{surname}{random.randint(0, 9)}"# 语文、数学、英语成绩scores = [random.randint(1, 100) for _ in range(3)]total_score = sum(scores)average_score = total_score / len(scores)# 插入数据到工作表中new_row = [student_id, name] + scores + [total_score, average_score]ws.append(new_row)# 输出执行信息print(f"Generated data for student {i}: {student_id}, {name}, Scores: {scores}, Total: {total_score}, Average: {average_score}")# 保存工作簿wb.save(file_path)print("Data generation and file saving completed.")
第4步:运行脚本
第5步:查检生成的结果
打开程序执行后生成的结果文件,如下:
注意事项
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |