Dec-09-2021, 07:00 PM
I have several text files in a folder that I want to split by paragraph and convert into csv. Each text file is composed of several paragraphs and some paragraphs have several lines. Paragraphs are separated by 1 empty line.
Text file example:
" A very long story
and paragraph.
Paragraph with several lines.
More information here."
How I want my csv file to look like:
id, text
abc.txt, A very long story and paragraph.
abc.txt, Paragraph with several lines. More information here.
def.txt, Imagine there is another text file.
This is my code:
id, text
abc.txt, A very long story
and paragraph.
abc.txt, Paragraph with several lines.
More information here.
def.txt, Imagine there is another text file.
Text file example:
" A very long story
and paragraph.
Paragraph with several lines.
More information here."
How I want my csv file to look like:
id, text
abc.txt, A very long story and paragraph.
abc.txt, Paragraph with several lines. More information here.
def.txt, Imagine there is another text file.
This is my code:
import csv, os import glob os.chdir(path) with open('output.csv', 'w', newline="", encoding="utf-16") as f: output = csv.writer(f) output.writerow(['id', 'text']) for txt_file in glob.iglob('*.txt'): with open(txt_file, 'r') as txt: for line in txt.read().split("\n\n"): output.writerow([(txt_file), line])This is how my csv file looks now:
id, text
abc.txt, A very long story
and paragraph.
abc.txt, Paragraph with several lines.
More information here.
def.txt, Imagine there is another text file.