Download process.py from pandaall/mycode: direct link, hf CLI and curl.
- Browser
- Download file 2.02 kB
-
https://huggingface.co/datasets/pandaall/mycode/resolve/7ca80a2cdf11ec7d72878f2e1092001a74a367d4/process.py
- Command line
-
hf download hf://datasets/pandaall/mycode@7ca80a2cdf11ec7d72878f2e1092001a74a367d4/process.py
-
curl -L -o process.py https://huggingface.co/datasets/pandaall/mycode/resolve/7ca80a2cdf11ec7d72878f2e1092001a74a367d4/process.py
2.02 kB
| 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: | |
| # 如果整行不是有效的JSON,直接加入失败列表 | |
| failure_list.append(line) | |
| continue | |
| result_str = data.get('result', '') | |
| # 去除“```json”开头和“```”结尾 | |
| 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', '') | |
| # 现在尝试解析为JSON | |
| try: | |
| result_json = json.loads(result_str) | |
| # 确保 result_json 是一个列表 | |
| if not isinstance(result_json, list): | |
| result_json = [result_json] | |
| data['result'] = result_json | |
| success_list.append(data) | |
| except json.JSONDecodeError as e: | |
| # 解析失败,将原始的line加入失败列表 | |
| 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') |