45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
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("生成完成!")
|