#!/usr/bin/env python3 import json import os from datetime import datetime # 读取视觉设计岗位简历.json文件 json_file = '网页未导入数据/视觉设计产业/视觉设计岗位简历.json' mock_file = 'src/mocks/resumeInterviewMock.js' print("正在读取包装设计师的面试题...") with open(json_file, 'r', encoding='utf-8') as f: positions_data = json.load(f) # 提取包装设计师的面试题 package_questions = [] for position in positions_data: if position.get("岗位名称") == "包装设计师": interview_content = position.get("面试题内容", "") if interview_content: lines = interview_content.split('\n') current_question = None current_answer = '' collecting_answer = False for line in lines: line_stripped = line.strip() # 检测问题行 if line_stripped and line_stripped[0].isdigit() and ('问题' in line_stripped or ':' in line_stripped): # 保存上一个问题 if current_question: answer = current_answer.strip() if len(answer) > 400: answer = answer[:400] + "..." package_questions.append({ 'question': current_question, 'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。" }) # 提取新问题 if '问题:' in line_stripped: current_question = line_stripped.split('问题:')[1].strip() elif '问题:' in line_stripped: current_question = line_stripped.split('问题:')[1].strip() elif '. ' in line_stripped: parts = line_stripped.split('. ', 1) if len(parts) > 1: current_question = parts[1].strip() current_answer = '' collecting_answer = False # 检测答案开始 elif any(marker in line for marker in ['参考回答:', '参考答案:', '答案:']): collecting_answer = True for marker in ['参考回答:', '参考答案:', '答案:']: if marker in line: answer_part = line.split(marker, 1)[1].strip() if answer_part: current_answer = answer_part break # 收集答案内容 elif collecting_answer and line.strip(): if line_stripped.startswith('#') or (line_stripped[0].isdigit() if line_stripped else False): if '问题' in line_stripped: collecting_answer = False else: if current_answer: current_answer += ' ' current_answer += line.strip() # 保存最后一个问题 if current_question: answer = current_answer.strip() if len(answer) > 400: answer = answer[:400] + "..." package_questions.append({ 'question': current_question, 'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。" }) break # 限制为16个问题 if len(package_questions) > 16: package_questions = package_questions[:16] print(f"找到 {len(package_questions)} 个面试题") # 创建备份 backup_file = f"{mock_file}.backup_package_{datetime.now().strftime('%Y%m%d_%H%M%S')}" with open(mock_file, 'r', encoding='utf-8') as f: content = f.read() with open(backup_file, 'w', encoding='utf-8') as f: f.write(content) print(f"已创建备份:{backup_file}") # 构建新的questions数组内容 sub_questions = [] for i, qa in enumerate(package_questions, 1): sub_questions.append({ "id": f"q2_{i}", "question": qa['question'].replace('"', '\\"').replace('\n', ' '), "answer": qa['answer'].replace('"', '\\"').replace('\n', ' ') }) new_questions = [{ "id": "group_q2", "question": "# 包装设计面试题", "subQuestions": sub_questions }] # 生成格式化的JSON字符串 questions_str = json.dumps(new_questions, ensure_ascii=False, indent=2) # 调整缩进为6个空格 lines = questions_str.split('\n') formatted_lines = [] for line in lines: formatted_lines.append(' ' + line if line.strip() else '') formatted_str = '\n'.join(formatted_lines).strip() # 要替换的内容模板 old_questions = '''[ { "id": "group_q2", "question": "# 一、专业能力与行业认知", "subQuestions": [ { "id": "q2_1", "question": "包装设计类岗位的核心职责是什么?", "answer": "负责包装设计相关工作,包括设计规划、创意实现、项目协作等。" } ] } ]''' # 执行替换 if old_questions in content: new_content = content.replace(old_questions, formatted_str) # 写入文件 with open(mock_file, 'w', encoding='utf-8') as f: f.write(new_content) # 验证语法 result = os.popen(f'node -c {mock_file} 2>&1').read() if result: print(f"❌ 语法错误:{result[:200]}") # 恢复备份 with open(backup_file, 'r', encoding='utf-8') as f: content = f.read() with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) print("已恢复备份") else: print("✓ 语法验证通过") print(f"成功更新包装设计的 {len(sub_questions)} 个面试题!") else: print("未找到要替换的内容,尝试其他方法...") # 使用更灵活的查找方式 import re pattern = r'("name":\s*"包装设计".*?"questions":\s*\[)[^\]]+\]' match = re.search(pattern, content, re.DOTALL) if match: # 替换questions数组 new_content = content[:match.end(1)] + formatted_str[1:-1] + ']' + content[match.end():] # 写入文件 with open(mock_file, 'w', encoding='utf-8') as f: f.write(new_content) # 验证语法 result = os.popen(f'node -c {mock_file} 2>&1').read() if result: print(f"❌ 语法错误:{result[:200]}") # 恢复备份 with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) print("已恢复备份") else: print("✓ 语法验证通过") print(f"成功更新包装设计的 {len(sub_questions)} 个面试题!") else: print("无法找到包装设计的questions字段")