#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 替换目标岗位优先级板块的数据 根据能源岗位简历.json中的批次信息分组岗位 """ import json import re from datetime import datetime def extract_positions_by_batch(energy_data): """从能源数据中提取并按批次分组岗位""" batch1 = [] batch2 = [] batch3 = [] for item in energy_data: position_name = item["岗位名称"] batch = item.get("批次", "") if "第一批次" in batch: batch1.append(position_name) elif "第二批次" in batch: batch2.append(position_name) elif "第三批次" in batch: batch3.append(position_name) return batch1, batch2, batch3 def update_target_position_file(batch1, batch2, batch3): """更新TargetPosition组件文件中的岗位数据""" file_path = 'src/pages/JobStrategyDetailPage/components/TargetPosition/index.jsx' # 创建备份 backup_path = f'{file_path}.backup_{datetime.now().strftime("%Y%m%d_%H%M%S")}' print(f"读取文件: {file_path}") with open(file_path, 'r', encoding='utf-8') as f: content = f.read() print(f"备份原文件到: {backup_path}") with open(backup_path, 'w', encoding='utf-8') as f: f.write(content) # 构建新的批次数据字符串 batch1_str = ',\n '.join([f'"{pos}"' for pos in batch1]) batch2_str = ',\n '.join([f'"{pos}"' for pos in batch2]) batch3_str = ',\n '.join([f'"{pos}"' for pos in batch3]) new_batch_data = f''' // 定义三个批次的岗位数据 const initialBatchPositions = {{ batch1: [ {batch1_str} ], batch2: [ {batch2_str} ], batch3: [ {batch3_str} ] }};''' # 使用正则表达式替换 pattern = r' // 定义三个批次的岗位数据\n const initialBatchPositions = \{[\s\S]*?\n \};' if re.search(pattern, content): new_content = re.sub(pattern, new_batch_data, content) print("✅ 成功匹配并替换岗位数据") else: print("⚠️ 未找到匹配的数据结构,尝试其他模式...") # 尝试另一种模式 pattern2 = r'const initialBatchPositions = \{[\s\S]*?\};' if re.search(pattern2, content): new_content = re.sub(pattern2, f'const initialBatchPositions = {{\n batch1: [\n {batch1_str}\n ],\n batch2: [\n {batch2_str}\n ],\n batch3: [\n {batch3_str}\n ]\n }}', content) print("✅ 使用备选模式成功替换") else: print("❌ 无法找到要替换的数据结构") return False # 写回文件 print(f"写入更新后的内容到: {file_path}") with open(file_path, 'w', encoding='utf-8') as f: f.write(new_content) return True def main(): # 读取能源岗位数据 energy_data_path = '网页未导入数据/能源产业/能源岗位简历.json' print(f"读取能源岗位数据: {energy_data_path}") with open(energy_data_path, 'r', encoding='utf-8') as f: energy_data = json.load(f) print(f"共读取到 {len(energy_data)} 个岗位") # 提取并分组岗位 batch1, batch2, batch3 = extract_positions_by_batch(energy_data) print(f"\n岗位分组结果:") print(f" 第一批次: {len(batch1)} 个岗位") print(f" 第二批次: {len(batch2)} 个岗位") print(f" 第三批次: {len(batch3)} 个岗位") # 显示每批次前3个岗位 print(f"\n第一批次示例: {batch1[:3] if batch1 else '无'}") print(f"第二批次示例: {batch2[:3] if batch2 else '无'}") print(f"第三批次示例: {batch3[:3] if batch3 else '无'}") # 更新组件文件 if update_target_position_file(batch1, batch2, batch3): print("\n✅ 目标岗位优先级数据替换完成!") print(f" - 第一批次: {len(batch1)} 个岗位") print(f" - 第二批次: {len(batch2)} 个岗位") print(f" - 第三批次: {len(batch3)} 个岗位") else: print("\n❌ 替换失败,请检查文件格式") if __name__ == "__main__": main()