u
This commit is contained in:
1 parent
a917be08d1
commit
a9ea500cd4
12 files changed
+18209
-21
No files matched your search
@@ -1,33 +1,44 @@
|
||||
import pandas as pd
|
||||
import random
|
||||
import string
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# 配置
|
||||
rows = 10000
|
||||
cols = 20
|
||||
rows = 4000
|
||||
|
||||
# 生成列名
|
||||
columns = [f"列{i+1}" for i in range(cols)]
|
||||
# 列名
|
||||
columns = ["箱号", "箱型", "制造日期"]
|
||||
|
||||
# 箱型列表
|
||||
container_types = ["20GP", "40GP", "40HQ", "45HQ"]
|
||||
|
||||
# 随机日期函数(生成 2000-01-01 到 2024-12-31)
|
||||
def random_date():
|
||||
start_date = datetime(2000, 1, 1)
|
||||
end_date = datetime(2024, 12, 31)
|
||||
delta = end_date - start_date
|
||||
rand_days = random.randint(0, delta.days)
|
||||
return (start_date + timedelta(days=rand_days)).strftime("%Y-%m-%d")
|
||||
|
||||
# 随机生成箱号(4字母 + 7数字)
|
||||
def random_conno():
|
||||
prefix = ''.join(random.choices(string.ascii_uppercase, k=4))
|
||||
number = ''.join(random.choices(string.digits, k=7))
|
||||
return prefix + number
|
||||
|
||||
# 生成数据
|
||||
data = []
|
||||
for _ in range(rows):
|
||||
row = []
|
||||
for _ in range(cols):
|
||||
# 随机数字或者随机字符串
|
||||
if random.random() < 0.5:
|
||||
row.append(random.randint(1, 10000)) # 随机数字
|
||||
else:
|
||||
row.append(''.join(random.choices(string.ascii_uppercase, k=5))) # 随机5个字母
|
||||
data.append(row)
|
||||
for i in range(rows):
|
||||
箱号 = random_conno()
|
||||
箱型 = random.choice(container_types)
|
||||
制造日期 = random_date()
|
||||
|
||||
data.append([箱号, 箱型, 制造日期])
|
||||
|
||||
# 创建 DataFrame
|
||||
df = pd.DataFrame(data, columns=columns)
|
||||
|
||||
# 保存 Excel
|
||||
df.to_excel("随机20列10000行.xlsx", index=False)
|
||||
|
||||
# 或者保存 CSV
|
||||
df.to_csv("随机20列10000行.csv", index=False, encoding='utf-8-sig')
|
||||
# 保存 Excel 和 CSV
|
||||
df.to_excel("箱数据4000行2.xlsx", index=False)
|
||||
df.to_csv("箱数据4000行2.csv", index=False, encoding="utf-8-sig")
|
||||
|
||||
print("生成完成!")
|
||||
Reference in new issue
Block a user