69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
import re
|
||
|
|
|
||
|
|
# 读取文件
|
||
|
|
with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f:
|
||
|
|
content = f.read()
|
||
|
|
|
||
|
|
# 备份
|
||
|
|
import datetime
|
||
|
|
backup_time = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
||
|
|
with open(f'src/mocks/resumeInterviewMock.js.backup_quotes_{backup_time}', 'w', encoding='utf-8') as f:
|
||
|
|
f.write(content)
|
||
|
|
|
||
|
|
# 修复所有在 question 和 answer 字段中的未转义引号
|
||
|
|
# 匹配 "question": "..." 或 "answer": "..." 中的内容
|
||
|
|
def fix_quotes_in_field(match):
|
||
|
|
field = match.group(1) # question 或 answer
|
||
|
|
value = match.group(2) # 字段值
|
||
|
|
|
||
|
|
# 替换内部的引号(不包括开头和结尾的引号)
|
||
|
|
# 查找所有未转义的引号
|
||
|
|
fixed_value = value
|
||
|
|
|
||
|
|
# 处理中文引号和英文引号
|
||
|
|
# 首先替换所有未转义的英文引号为转义的
|
||
|
|
parts = []
|
||
|
|
i = 0
|
||
|
|
while i < len(fixed_value):
|
||
|
|
if i > 0 and fixed_value[i] == '"' and fixed_value[i-1] != '\\':
|
||
|
|
parts.append('\\"')
|
||
|
|
else:
|
||
|
|
parts.append(fixed_value[i])
|
||
|
|
i += 1
|
||
|
|
|
||
|
|
if parts:
|
||
|
|
fixed_value = ''.join(parts)
|
||
|
|
|
||
|
|
return f'"{field}": "{fixed_value}"'
|
||
|
|
|
||
|
|
# 使用更精确的正则表达式
|
||
|
|
pattern = r'"(question|answer)":\s*"([^"\\]*(\\.[^"\\]*)*)"'
|
||
|
|
|
||
|
|
# 分块处理避免递归深度问题
|
||
|
|
lines = content.split('\n')
|
||
|
|
fixed_lines = []
|
||
|
|
|
||
|
|
for line in lines:
|
||
|
|
if '"question":' in line or '"answer":' in line:
|
||
|
|
# 检查是否有中文引号
|
||
|
|
if '"' in line or '"' in line:
|
||
|
|
# 替换中文引号
|
||
|
|
line = line.replace('"', '\\"').replace('"', '\\"')
|
||
|
|
|
||
|
|
# 修复可能的双转义
|
||
|
|
line = line.replace('\\\\"', '\\"')
|
||
|
|
line = line.replace(': "\\"', ': "') # 修复开头
|
||
|
|
line = line.replace('\\"",', '",') # 修复结尾
|
||
|
|
line = line.replace('\\""', '"') # 修复结尾2
|
||
|
|
|
||
|
|
fixed_lines.append(line)
|
||
|
|
|
||
|
|
content = '\n'.join(fixed_lines)
|
||
|
|
|
||
|
|
# 保存修复后的文件
|
||
|
|
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
|
||
|
|
f.write(content)
|
||
|
|
|
||
|
|
print("引号修复完成!")
|