#!/usr/bin/env python3 # -*- coding: utf-8 -*- import pandas as pd import json from pathlib import Path def test_excel_parsing(): """测试Excel文件解析""" excel_path = "敌人配置表.xlsx" sheet_name = "敌人基础配置" print(f"正在读取Excel文件: {excel_path}") try: # 读取Excel文件 df = pd.read_excel(excel_path, sheet_name=sheet_name) print(f"成功读取Excel文件,共{len(df)}行,{len(df.columns)}列") # 打印列名 print(f"列名: {list(df.columns)}") # 打印前几行数据 print("\n前5行数据:") for i in range(min(5, len(df))): print(f"第{i}行: {df.iloc[i].to_dict()}") # 模拟配置解析逻辑 param_types = { '敌人ID': str, '敌人名称': str, '敌人类型': str, '稀有度': str, '权重': int, '生命值': int, '移动速度': int, '攻击力': int, '攻击范围': int, '攻击速度': float, '防御力': int, '金币奖励': int } # 检查第1行是否为描述行 data_start_row = 1 if len(df) > 1: first_cell = str(df.iloc[1, 0]).strip() print(f"第1行第一个单元格: '{first_cell}'") # 如果第1行第一个单元格是描述性文字,则从第2行开始 if first_cell in ['唯一标识符', '描述', 'description', 'desc']: data_start_row = 2 print("检测到描述行,跳过第1行") else: data_start_row = 1 print("第1行是数据行,从第1行开始") print(f"\n数据起始行: {data_start_row}") # 解析多行数据 config_list = [] for i in range(data_start_row, len(df)): row_config = {} print(f"\n处理第{i}行数据:") for col_idx, col_name in enumerate(df.columns): param_name = str(col_name).strip() print(f" 列{col_idx}: {param_name}") if param_name in param_types: try: param_value = df.iloc[i, col_idx] print(f" 原始值: {param_value} (类型: {type(param_value)})") if pd.isna(param_value): print(f" 跳过空值") continue param_type = param_types[param_name] if param_type == bool: row_config[param_name] = str(param_value).lower() in ['true', '1', 'yes', 'on'] else: row_config[param_name] = param_type(param_value) print(f" 转换后: {row_config[param_name]}") except (ValueError, TypeError, IndexError) as e: print(f" 转换失败: {e}") continue else: print(f" 参数名不在类型映射中") if row_config: # 只添加非空配置 config_list.append(row_config) print(f" 添加配置: {row_config}") else: print(f" 跳过空配置") print(f"\n最终解析结果: 共{len(config_list)}个配置项") for i, config in enumerate(config_list): print(f"配置{i}: {config}") # 转换为敌人数据格式 print("\n转换为敌人数据格式:") enemies = [] for item in config_list: enemy_data = { 'id': item.get('敌人ID', ''), 'name': item.get('敌人名称', ''), 'type': item.get('敌人类型', ''), 'rarity': item.get('稀有度', ''), 'weight': item.get('权重', 1), 'health': item.get('生命值', 100), 'speed': item.get('移动速度', 50), 'attack': item.get('攻击力', 10), 'range': item.get('攻击范围', 100), 'attackSpeed': item.get('攻击速度', 1.0), 'defense': item.get('防御力', 0), 'goldReward': item.get('金币奖励', 10) } enemies.append(enemy_data) print(f"敌人数据: {enemy_data}") except Exception as e: print(f"解析失败: {e}") import traceback traceback.print_exc() if __name__ == "__main__": test_excel_parsing()