Start New Discussion within our Software Development Community

:mrgreen: hi guys/gals,
thanks for the bioinformatics help, im working on tons of independant projects, it feels like im in summer school and im not even in it! oh well, that is how it is. Python and me are like new friends so I have been needing on this journey and appreciate it so to the question:

I want to input a file that have a few sentences and a blank line after a sentence:

a sentence one

a sentence two

and i want to out put each separate sentence in a text file
so a sentence one--> one.txt
so a sentence two-->two.txt

I have this so far:

#now to output something we have gathered from a file in our directory into a new file
#I will take as input the 'my_text.txt' file and then I will output this into a new file called
#'newdave.txt'
 
myfile=open('my_text.txt') 
#Again, just opening the file and taking its data
 
myfile.seek(0)
text = myfile.read() 
#store all the 'my_text.txt' data into the string object 'text'
 
#Now to create the newfile 'newdave' and write our 'text' 
newfile = open('newdave.txt', 'w') 
#open the new file and set it with 'w' for "write"
 
#loop trough the 'text' lines and write them into the 'newfile'
for line in text:
newfile.write(line)
 
newfile.close()

Right now it take the whole file and puts it into another file... thanks for you help!

You mean something like that:

# write out each text containing line 
# (other than '\n') to a separate file

str1 = """sentence one

sentence two

sentence three

"""

# write str1 to the test file
fout = open("test.txt", "w")
fout.write(str1)
fout.close()

# read the test file as a list of lines
fin = open("test.txt", "r")
list1 = fin.readlines()
fin.close()

print list1  # just testing

n = 0
for line in list1:
    if len(line) > 1:
        n += 1
        filename = "test%d.txt" % n
        fout = open(filename, "w")
        fout.write(line)
        fout.close()
        print "%s --> %s" % (line, filename)  # just testing
This question has already been answered. Start a new discussion instead.