tolidtm at gmail.com
Fri Feb 3 22:03:04 CET 2012
Thanks guys that was fast:
for line in users:
fob.write(line + "\n")
and that works
Excuse me for the stupid questions, but since one week I read alot of
python and I`m confused :p
the only program language I knew in the time was Pascal, but i forgot all
On Fri, Feb 3, 2012 at 9:56 PM, Dave Angel <d at davea.name> wrote:
> On 02/03/2012 03:27 PM, Anatoli Hristov wrote:
>> Hi everyone,
>> I`m totaly new in python and trying to figure out - how to write a list to
>> a file with a newline at the end of each object.
>> I tried alot of combinations :) like:
>> users = ['toli','didi']
>> fob.writelines(users) + '%s\N'
>> or fob.writelines('\N' % users)
>> or fob.writelines('%s\N' % users)
>> but nothing of dose works...
>> Could you help me find out the right syntaxes?
>> mylist.writelines() is a shorthand for a loop of writes, once per list
> item. It does not append a newline, since if the list had come from
> readlines(), it would already have the linefeed on each line.
> So you have a few choices. You could add a newline to each list item
> before issuing the writelines(), or write your own loop. I vote for
> writing your own loop, since there may be other things you want to change
> on each line.
> users = [item+"\n" for item in users] # add a newline to each item
> for line in users:
> fob.write(line + "\n")
> There are other possibilities, such as
> contents = "\n".join(mylist) #make a single string out of it
> fob.write(contents + "\n") #note we had to add one at the very
> #because join just puts the separator between items, not after
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list