"
1.脚本:[Python] 纯文本查看 复制代码#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pandas as pd
import numpy as np
import json
import string
import secrets
# 定义生成随机字符串的函数
def generate_random_string(length):
letters = string.ascii_letters # 包含大小写字母的字符串
return ''.join(secrets.choice(letters) for _ in range(length))
def random_split_float(total, n):
# 生成 n-1 个随机小数并保留两位小数
random_floats = np.round(np.random.rand(n - 1) * total, 2)
# 计算这些随机小数的和
sum_of_floats = np.sum(random_floats)
# 计算最后一个小数
last_value = np.round(total - sum_of_floats, 2)
# 如果最后一个小数小于0或大于总数(极端情况),重新生成
while last_value total:
random_floats = np.round(np.random.rand(n - 1) * total, 2)
sum_of_floats = np.sum(random_floats)
last_value = np.round(total - sum_of_floats, 2)
# 将最后一个小数加入数组
random_floats = np.append(random_floats, last_value)
return random_floats
def random_split(total, n):
# 生成 n-1 个随机点
split_points = sorted(np.random.randint(1, total, n-1))
# 将起始点和结束点加入切分点
split_points = [0] + split_points + [total]
# 计算每段的值
segments = [split_points[i+1] - split_points for i in range(n)]
return segments
def genExcel():
# 自定义列字段
with open('config.json', 'r') as f:
dataj = json.load(f)
allcolumn = [column['name'] for column in dataj['columns']]
# 随机生成N行数据
N = dataj["number"] # 生成N行数据
data = {}
for column in dataj["columns"]:
if(column["type"] == 0):
data[column["name"]] = np.random.choice(column["valueArea"], N)
if(column["type"] == 1):
data[column["name"]] = np.random.randint(column["valueArea"][0], column["valueArea"][1], N)
if (column["type"] == 2):
data[column["name"]] = np.round(np.random.uniform(column["valueArea"][0], column["valueArea"][1], N),2)
if(column["type"] == 3):
if(type(column["length"]) == int):
data[column["name"]] = [generate_random_string(column["length"]) for _ in range(N)]
else:
data[column["name"]] = [generate_random_string(np.random.randint(column["length"][0], column["length"][1])) for _ in range(N)]
if(column["type"] == 4):
emum = dataj["enums"][column["parent"]]
data[column["name"]] = []
for i in range(N):
for item in emum:
if(data[column["parent"]] == item["value"]):
data[column["name"]].append(np.random.choice(item["children"]))
if(column["type"] == 5):
data[column["name"]] = np.round(np.random.uniform(column["valueArea"][0], column["valueArea"][1], N),2)
for i in range(len(column["parameters"])):
allcolumn.append(column["parameters"])
data[column["parameters"]] = []
for i in range(N):
paramlist = random_split_float(data[column["name"]],len(column["parameters"]))
for j in range(len(column["parameters"])):
data[column["parameters"][j]].append(paramlist[j])
# 创建DataFrame
df = pd.DataFrame(data, columns=allcolumn)
# 将DataFrame写入Excel文件
df.to_excel('random_data.xlsx', index=False)
2.json配置文件:
[Asm] 纯文本查看 复制代码{
"columns": [
{
"name":"id",
"type": 3,
"length": 32
},
{
"name":"NickName",
"type": 3,
"length": [0,2]
},
{
"name":"Name",
"type": 0,
"valueArea": [
"Alice", "Bob", "Charlie", "David", "Eva"
]
},
{
"name":"Age",
"type": 1,
"valueArea": [20, 60]
},
{
"name":"City",
"type": 0,
"valueArea": [
"Alice", "Bob", "Charlie", "David", "Eva"
]
},
{
"name":"Salary",
"type": 2,
"valueArea": [30000.0, 100000.0]
},
{
"name":"Street",
"type": 4,
"parent": "City"
},
{
"name":"sum",
"type": 5,
"parameters": ["x","y","z"],
"valueArea": [5000.0, 10000.0]
}
],
"number": 1000,
"enums": {
"City":[
{
"value": "Alice",
"children": ["a","b","c"]
},
{
"value": "Bob",
"children": ["d","e","f"]
},
{
"value": "Charlie",
"children": ["g","h","i"]
},
{
"value": "David",
"children": ["j","k","l"]
},
{
"value": "Eva",
"children": ["m","n","o"]
}
]
}
}
3.配置规则说明:
[table][tr][td]type