rbxIdleWeapon/export.py
2025-07-02 00:21:32 +08:00

91 lines
3.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 os
import json
import openpyxl
import re
excel_dir = 'excel'
output_dir = 'src/ReplicatedStorage/Json'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
def parse_array_field(value, elem_type):
"""
解析数组类型字段,支持[1,2,3]或1,2,3写法自动去除空格和空字符串。
elem_type: 'int', 'float', 'string'
"""
if value is None:
return []
s = str(value).strip()
# 支持带中括号写法
if s.startswith("[") and s.endswith("]"):
s = s[1:-1]
# 支持英文逗号和中文逗号
items = re.split(r'[,]', s)
result = []
for v in items:
v = v.strip()
if v == "":
continue
if elem_type == "int":
try:
result.append(int(v))
except Exception:
pass
elif elem_type == "float":
try:
result.append(float(v))
except Exception:
pass
else:
result.append(v)
return result
for filename in os.listdir(excel_dir):
if filename.endswith('.xlsx'):
filepath = os.path.join(excel_dir, filename)
wb = openpyxl.load_workbook(filepath)
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
rows = list(ws.iter_rows(values_only=True))
if len(rows) < 2:
continue
headers = rows[0]
types = rows[1]
# 只保留字段名和类型都不为空的字段
valid_indices = [
i for i, (h, t) in enumerate(zip(headers, types))
if h is not None and str(h).strip() != "" and t is not None and str(t).strip() != ""
]
valid_headers = [headers[i] for i in valid_indices]
valid_types = [types[i] for i in valid_indices]
data = []
for row in rows[2:]:
filtered_row = [row[i] if i < len(row) else None for i in valid_indices]
row_dict = {}
for h, t, v in zip(valid_headers, valid_types, filtered_row):
if t.endswith("[]"):
elem_type = t[:-2]
row_dict[h] = parse_array_field(v, elem_type)
else:
row_dict[h] = v
id_value = row_dict.get("id")
# 只导出id为数字且不为空的行
if id_value is not None and isinstance(id_value, (int, float)) and str(int(id_value)).isdigit():
data.append(row_dict)
if not data:
continue
out_name = f"{sheet_name}.json"
out_path = os.path.join(output_dir, out_name)
# 写入json每个对象单独一行
with open(out_path, 'w', encoding='utf-8') as f:
f.write('[\n')
for i, obj in enumerate(data):
line = json.dumps(obj, ensure_ascii=False, separators=(',', ':'))
if i < len(data) - 1:
f.write(line + ',\n')
else:
f.write(line + '\n')
f.write(']')
print(f"导出: {out_path}")