import re import csv import json import pandas as pd # df = pd.read_parquet('train-00000-of-00001-288b64d2a0003a2f.parquet') # 将数据框保存为CSV文件,并设置escapechar参数 # df.to_csv('train_triviaqa.csv',encoding="utf-8",escapechar='\\') csv_file = 'train_triviaqa.csv' jsonl_file = 'TriviaQA_train.jsonl' # 生成JSONL文件 messages = [] with open(csv_file, 'r', encoding='utf-8') as file: reader = csv.reader((line.replace('\0', '') for line in file)) next(reader) # 跳过标题行 for row in reader: if len(row) >= 4: context = row[1] question = row[2] answers = row[3] answer = answers message={ "instruction":"As a reading comprehension expert, you will receive context and question. Please understand the given Context first and then output the answer of the question based on the Context","input": str({'context':{context},'question':{question},}),"output":f'{answer}'} messages.append(message) # 保存为JSONL文件 with open(jsonl_file, 'w', encoding='utf-8') as file: for message in messages: file.write(json.dumps(message, ensure_ascii=False) + '\n')