0

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.

enter image description here

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@ioio Is this what you were looking for?
I could probably make it work, but database.remove(' ') generates an error. And I need the split blocks in separate output files. Thank for your input.
@ioio You can just remove that part and it should still work. I had been using it for my test file since some of the outputs had come up with nothing.
I deleted the offending part. I think I can work this out from here with a bit more thinking :)
@ioio It works fine for me, creating a new file per block, not sure why it wouldn't work otherwise. You will be able to figure it out though, have fun!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.