{ "cells": [ { "metadata": { "ExecuteTime": { "end_time": "2024-09-08T15:26:59.355018Z", "start_time": "2024-09-08T15:26:57.990909Z" } }, "cell_type": "code", "source": [ "import pandas as pd\n", "\n", "# 定义一个函数,将 Options 字符串转换为 key-value 格式的列表\n", "def format_options(options_str):\n", " options_list = []\n", " # 按行分割选项\n", " options_lines = options_str.split(\"\\n\")\n", " for line in options_lines:\n", " if len(line) > 1:\n", " key = line[0] # 第一个字符为选项字母\n", " value = line[2:].strip() # 从第三个字符开始为选项内容\n", " options_list.append({\"key\": key, \"value\": value})\n", " return options_list\n", "\n", "# 读取 CSV 文件\n", "train_data = pd.read_csv('./data/train.csv', encoding='utf-8')\n", "valid_data = pd.read_csv('./data/val.csv', encoding='utf-8')\n", "test_data = pd.read_csv('./data/test_with_annotations.csv', encoding='utf-8')\n", "\n", "# 将json数据只保留与train和valid一致的字段\n", "test_data = test_data[['Question', 'Options', 'Answer', 'Explanation']]\n", "\n", "# 遍历数据集,将每个样本的 Options 列格式化\n", "train_data['Options'] = train_data['Options'].apply(format_options)\n", "valid_data['Options'] = valid_data['Options'].apply(format_options)\n", "test_data['Options'] = test_data['Options'].apply(format_options)\n", "\n", "# 将修改后的 DataFrame 保存为 JSON 文件\n", "train_data.to_json('./data/train.json', orient='records', lines=True, force_ascii=False)\n", "valid_data.to_json('./data/valid.json', orient='records', lines=True, force_ascii=False)\n", "test_data.to_json('./data/test.json', orient='records', lines=True, force_ascii=False)\n" ], "id": "c003560dea95e12e", "outputs": [], "execution_count": 5 }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "8d142ed01196946" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }