coverting list back to string

Andrew Bennetts andrew-pythonlist at puzzling.org
Tue Apr 22 16:08:47 CEST 2003


On Mon, Apr 21, 2003 at 12:32:11PM -0700, scn wrote:
> my code:
> 
> import xreadlines
> 
> input = open('c:/python22/programs/linetest.txt', 'r') # 3 lines of
> text
> 
> output = open('c:/python22/programs/output.txt', 'w')
> 
> for line in input.xreadlines():
>     remove = line.split()
>     remove[0:1] = []
>     output.write(remove)
> 
> output.close()
> input.close()
> 
> my quest:  i know that output.write(remove) is not correct.  you
> cannot use the 'write' method on a list.  is there a better way to
> convert the list to a string after the first element of the list has
> been removed and then output the strings to a file?  i'm a newbie
> programmer and need guru help. thank you.

Simply don't split more than you need to:

    # ...

    for line in input.xreadlines():
        # Split on whitespace a maximum of one time
        discard, rest = (line.split(None, 1) + [''])[:2]
        output.write(rest)
    
    # ...

-Andrew.






More information about the Python-list mailing list