I have a large text file full of notes that I would like to split and separate into individual rows using Python. I've gotten it to work somewhat, but it is adding one letter per cell in a .csv file, not the entire section. I've inserted the @@@ characters to denote where each section needs to be split. For example, here's what my .txt file looks like:
@@@ jlkdlkjdlkjdalkjdalk @@@ 78278947298729874298742 @@@ llkdlaklkdalkdsa
@@@ nmczxmnczxmncz
I eventually want it exported into .csv so it would look like this:
ID | Reporttext
1 | jlkdlkjdlkjdalkjdalk
2 | 78278947298729874298742
3 | llkdlaklkdalkdsa
4 | nmczxmnczxmncz
Right now it's being exported like this: j l k d l k (and so on).
Here's my code:
import re, csv with open("thetext.txt") as f: for line in f: for word in line.split("@@@"): with open(r'theoutput.csv', 'a') as g: writer = csv.writer(g) writer.writerow(word) print(word)
So just to reiterate, my problem is avoiding the spacing (e.g., t h i s ) when it exports.
Thanks!
data = pd.read_csv('my_file.txt', sep="@@@ ", header=None)pd.to_csv('my_new_file.csv')