File size: 966 Bytes
f8e73ae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
"""
csv_to_jsonl.py
Converts a CSV file with columns `prompt` and `response` into a JSONL file
where each line is a JSON object: {"input": "<prompt>", "output": "<response>"}
Run:
python csv_to_jsonl.py
This will create `dataset.jsonl` in the same folder.
"""
import csv
import json
INPUT_CSV = "dataset.csv"
OUTPUT_JSONL = "dataset.jsonl"
def csv_to_jsonl(in_csv: str = INPUT_CSV, out_jsonl: str = OUTPUT_JSONL):
with open(in_csv, "r", encoding="utf-8") as f_in, open(out_jsonl, "w", encoding="utf-8") as f_out:
reader = csv.DictReader(f_in)
# Ensure header contains prompt and response
if "prompt" not in reader.fieldnames or "response" not in reader.fieldnames:
raise ValueError("CSV must contain 'prompt' and 'response' columns")
for row in reader:
obj = {"input": row["prompt"].strip(), "output": row["response"].strip()}
f_out.write(json.dumps(obj, ensure_ascii=False) + "\n")
print(f"Wrote {out_jsonl}")
if __name__ == "__main__":
csv_to_jsonl() |