list to string

Dave Reed dreed at capital.edu
Tue Jul 23 18:43:08 EDT 2002


Wouldn't

all_text = file.read()

do what you want instead of reading the lines and then joining them?

Dave


> From: Michael Gilfix <mgilfix at eecs.tufts.edu>
> 
>   It sounds like you want to do something like this:
> 
> list = [ 'this', ' ', 'is', ' a', ' test' ]
> string = ''.join (list)
> 
>   Or if you want a space and you just have a list
> of strings:
> 
> string = ' '.join (list)
> 
>   The character in the source string is used to join the elements in
> the list of the join() call.  So in the context of what you're written
> here, you could do:
> 
>   file = open ('foo.txt', 'r')
>   lines = file.readlines ()
>   all_text = ''.join (lines)
> 
>                  -- Mike
> 
> On Tue, Jul 23 @ 13:08, David LeBlanc wrote:
> > I have this code:
> > 
> > fn = file('foo.txt')
> > 
> > txt = fn.readlines()
> > 
> > str = ?something?(txt) # or txt.?something?()...
> > 
> > What's the something? This is probably so obvious, i'm going to feel dumb,
> > but I havn't been able to find anything in any doc I've looked at.
> > 
> > David LeBlanc
> > Seattle, WA USA




More information about the Python-list mailing list