| import json |
|
|
| def process_jsonl(input_file, success_file, failure_file): |
| success_list = [] |
| failure_list = [] |
| with open(input_file, 'r', encoding='utf-8') as infile: |
| for line in infile: |
| line = line.strip() |
| if not line: |
| continue |
| try: |
| data = json.loads(line) |
| except json.JSONDecodeError: |
| |
| failure_list.append(line) |
| continue |
| result_str = data.get('result', '') |
| |
| if result_str.startswith("```json"): |
| result_str = result_str[len("```json"):].lstrip() |
| if result_str.endswith("```"): |
| result_str = result_str[:-len("```")].rstrip() |
| |
| |
| result_str = result_str.replace('\\"', '"').replace('\\\\', '\\') |
| result_str = result_str.replace('\\n', '') |
| |
| try: |
| result_json = json.loads(result_str) |
| |
| if not isinstance(result_json, list): |
| result_json = [result_json] |
| data['result'] = result_json |
| success_list.append(data) |
| except json.JSONDecodeError as e: |
| |
| failure_list.append(line) |
| |
| with open(success_file, 'w', encoding='utf-8') as sf: |
| for item in success_list: |
| sf.write(json.dumps(item, ensure_ascii=False) + '\n') |
| |
| with open(failure_file, 'w', encoding='utf-8') as ff: |
| for item in failure_list: |
| ff.write(item + '\n') |
|
|
| |
| process_jsonl('gpt4_query_correlation2.jsonl', 'success.jsonl', 'failure.jsonl') |