import os import json import pandas as pd folder_path = "双语数据\史记\七十列传" def get_files(folder_path): subfolders = [f.path for f in os.scandir(folder_path) if f.is_dir()] print(subfolders) dataset = [] source_file = "source.txt" target_file = "target.txt" for x in subfolders: with open(os.path.join(x,source_file) , "r", encoding="utf-8") as f: source_content = f.read() with open(os.path.join(x,target_file), "r", encoding="utf-8") as f: target_content = f.read() source_content = source_content.split("\n") target_content = target_content.split("\n") for i in range(len(source_content)): dataset.append([source_content[i], target_content[i]]) return dataset dataset = get_files(folder_path) # add one column "instruction" with the content "请把古文翻译成现代汉语" to the dataset df = pd.DataFrame(dataset, columns=["source", "target"]) df["instruction"] = "请把现代汉语翻译成古文" df.rename(columns={"source": "output", "target": "input"}, inplace=True) print(len(df)) df.to_json("dataset.jsonl", orient="records", lines=True, force_ascii=False)