import csv | |
# Replace 'input.txt' with the path to your input text file | |
input_file = 'works.txt' | |
# Replace 'output.csv' with the desired output CSV file path | |
output_file = 'train.csv' | |
# Read the text file and split it into paragraphs | |
with open(input_file, 'r', encoding='utf-8') as file: | |
paragraphs = file.read().split('\n\n') # Adjust the split pattern based on your paragraph format | |
# Create a CSV writer and write the paragraphs into the CSV file | |
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile: | |
csv_writer = csv.writer(csvfile) | |
# Use a loop to write prompt and continuation in separate columns | |
for i in range(len(paragraphs)): | |
prompt = ' '.join(paragraphs[i:i + 2]) # Combine two paragraphs for the prompt | |
continuation = ' '.join(paragraphs[i + 2:i + 7]) # Combine five paragraphs for the continuation | |
# Write the prompt and continuation in separate columns | |
csv_writer.writerow([prompt.strip(), continuation.strip()]) | |