954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

writing elements of a list to an outfile

Hi,

I am trying to write elements from a list that I have created to an outfile using a for-loop with this code:

for j in range(int(Allele[i])):
             outfile.write('             allelelocus[j]   *     ')

However when I do this I just get a whole bunch of allelelocus[j] in my out file. I was wondering if there was any way to get my program to write the actual jth element of the list and not just allelelocus[j]. Thanks!

Elise

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 
for j in range(int(Allele[i])):
    outfile.write('             %s   *     ' % allelelocus[j])
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 
for j in range(int(Allele[i])):
    outfile.write('             %s   *     ' % allelelocus[j])

Inserting %s didn't seem to do anything except put %s in my outfile. I think the problem is that the program is writing literally what I put in the command. So I need a way for it not to do that, if that's possible.

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 
Inserting %s didn't seem to do anything except put %s in my outfile. I think the problem is that the program is writing literally what I put in the command. So I need a way for it not to do that, if that's possible.

That means you did something wrong. Look at tonyjv's code again.

This is the newer way to do exactly the same thing:

for j in range(int(Allele[i])):
    outfile.write('             {0}     *     '.format(allelelocus[j]))
jcao219
Posting Pro in Training
417 posts since Dec 2009
Reputation Points: 28
Solved Threads: 97
 

Woops! Thanks so much!

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: