Files
workspace/code/base-project/base-app/test.py
T
2025-11-24 17:31:41 +08:00

45 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pandas as pd
import random
import string
from datetime import datetime, timedelta
rows = 4000
# 列名
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 i in range(rows):
箱号 = random_conno()
箱型 = random.choice(container_types)
制造日期 = random_date()
data.append([箱号, 箱型, 制造日期])
# 创建 DataFrame
df = pd.DataFrame(data, columns=columns)
# 保存 Excel 和 CSV
df.to_excel("箱数据4000行2.xlsx", index=False)
df.to_csv("箱数据4000行2.csv", index=False, encoding="utf-8-sig")
print("生成完成!")