[Tutor] output formatting

Dave Angel davea at ieee.org
Sat Apr 25 15:18:10 CEST 2009


Matt Domeier  wrote:

> Hello,
>
> I have a series of lists that I need to output into files with a  
> specific format. Specifically, I need to have line breaks after each  
> entry of the list, and I need to do away with the ['..'] that  
> accompany the data after I transform the list into a string. Can I  
> simply append a '\n' to the end of all the list entries in order to  
> enact line breaks?
>
> Thanks!
>
>   
First point:  you should include source code for what you're doing (cut 
'n paste, don't retype it).  Normally, it should also include sample 
data and/or sample results.

For example, you say you're transforming the list into a string, and 
that ".." accompanies the data.  I have to guess what you're doing, and 
therefore how to fis it.  I'd guess that for some reason you're using
      "..".join(mylist)

which would make a string by concatenating all the strings in the list, 
with ".." between them.  As Wayne has pointed out, you could just 
concatenate them using the "\n" separator instead of the ".."  But 
notice that puts newlines between the elements, but doesn't put one at 
the end.  Since he's using print, that automatically adds a trailing 
newline.   But probably you're not using print.  You didn't say.

But you say you want to put them into *files*, and "with a specific 
format."  It's only a wild guess that the plural was a typo, and that 
the specific format is called a text file.  Much more likely, you want 
to put some of them in one file, formatted a certain way, and some into 
another file, formatted differently.  If that's the case, why not loop 
through the list, doing whatever you really need?  You can always use a 
"\n" as part of the format when doing the call to write(), or whatever 
you're using to do the file I/O.  Or just use writeline(), which 
implicitly adds the newline.




More information about the Tutor mailing list