0

I'm splitting a file based on a string, and would like to have the output file names be numbered.

This is what I have so far:

outputfile = open("output.seq")   
outputfileContent = outputfile.read()
outputfileList = outputfileContent.split(">")
for count, line in enumerate(f):
    for items in outputfileList:
        seqInfoFile = open('%f.dat', 'w')
    seqInfoFile.write(str(items))

I'm not sure where to define f.

Thanks for any help!

2
  • How does output.seq look like? What do you want to accomplish? Commented Jul 25, 2012 at 13:56
  • what is f in that code Commented Jul 25, 2012 at 14:01

3 Answers 3

2

Assuming I haven't misunderstood you, where you have it.

outputfile = open("output.seq")   
outputfileContent = outputfile.read()
outputfileList = outputfileContent.split(">")

for count, content in enumerate(outputfileList, 1):
    with open("output_%s.dat" % count, "w") as output:
        output.write(content)
Sign up to request clarification or add additional context in comments.

1 Comment

I was about to post a similar thing. Simple one.
0

It would seem that if you want to associate every item in the output file list with a file titled as its index, you should do something like this:

for i in range(len(outputfileList)):
    seqInfoFile = open(str(i) + '.dat', 'w')
    seqInfoFile.write(str(outputfileList[i]))

It's not quite as elegant as an iterator, but the other option is to determine the number by making a call to outputfileList.index(items) each time.

Comments

0

Open output.seq, write its first line (splitted at >) into the file 1.dat, the second one to 2.dat and so on:

with open("output.seq") as fi:
    for count, line in enumerate(fi, 1):
        with open('{0}.dat'.format(count), 'w') as fo:
            fo.writelines(line.split('>'))

2 Comments

He sounds like he wants to output the split text, not each line.
@Julian - then let's split it…

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.