Reputation: 21
I have a text file that is formatted as shown below. I want to split each block of text starting from '# Data Written' into separate text files.
How do I go about this? I'm sure a few lines of python code could do this, but I'm not a pythonista, alas. Suggestions, please.
Upvotes: 0
Views: 507
Reputation: 136
I assume this is something along the lines of what you were looking for?
f = open('filename.txt', 'r')
databaseRaw = f.read()
database = databaseRaw.split('# Data Written')
f.close()
database.remove('')
for i in range(0, len(database)) :
database[i] = '# Data Written'+''.join(database[i])
for i in range(0, len(database)) :
f = open("output.txt"+ str([i]) ,"w+")
f.write(database[i])
f.close()
EDIT : Figured out the problem I had had before, works fine now.
it will create a new file per block, starting at 0, and if it creating a new line at the of end each file is an issue, I can make an easy way to remove it.
Upvotes: 2