import json input_file = "./dialogues.txt" output_file = "./dialogues.jsonl" with open(input_file, "r", encoding="utf-8") as f_in, \ open(output_file, "w", encoding="utf-8") as f_out: lines = f_in.readlines() i = 0 while i < len(lines): line = lines[i].strip() if line.startswith("C:"): prompt = line[2:].strip() # Look ahead for the next S: line j = i + 1 while j < len(lines) and not lines[j].strip().startswith("S:"): j += 1 if j < len(lines): response = lines[j].strip()[2:].strip() json_obj = {"prompt": prompt, "response": response} f_out.write(json.dumps(json_obj, ensure_ascii=False) + "\n") i = j + 1 else: i += 1 else: i += 1 print(f"Converted {input_file} → {output_file} successfully!")