Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Help writelines

12 views
Skip to first unread message

Markus Rother

unread,
Feb 3, 2012, 3:41:54 PM2/3/12
to pytho...@python.org
Hi,

You have to iterate.
Either with

for u in users:
fob.write( u + '\n' )

or with a lambda function.

always a good call: http://python.org/

greets,
M.

On 02/03/2012 09: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=open('c:/Python27/Toli/username','w')
> fob.writelines(users) + '%s\N'
> fob.close()
> 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?
>
> Thanks
>
>
>

Nick Dokos

unread,
Feb 3, 2012, 3:47:37 PM2/3/12
to Anatoli Hristov, pytho...@python.org
Anatoli Hristov <tol...@gmail.com> 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=open('c:/Python27/Toli/username','w')
> fob.writelines(users) + '%s\N'
> fob.close()
>  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?
>

>From the docs:

| writelines(...)
| writelines(sequence_of_strings) -> None. Write the strings to the file.
|
| Note that newlines are not added. The sequence can be any iterable object
| producing strings. This is equivalent to calling write() for each string.

So *you* need to add the newlines, e.g. you can use a list comprehension:

fob.writelines(["%s\n" % (x) for x in users])

or write in a loop:

for u in users:
fob.write("%s\n" % (u))

or join the list elements together with a newline separator (but you'll
need to add a final newline by hand):

fob.writelines("\n".join(users) + "\n")

or ...

Nick

Dave Angel

unread,
Feb 3, 2012, 3:56:29 PM2/3/12
to Anatoli Hristov, pytho...@python.org
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=open('c:/Python27/Toli/username','w')
> fob.writelines(users) + '%s\N'
> fob.close()
> 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?
>
> Thanks
>
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.

1)
users = [item+"\n" for item in users] # add a newline to each item

2)
for line in users:
fob.write(line + "\n")
fob.close()

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 end,
#because join just puts the separator between items, not
after them.




--

DaveA

Message has been deleted
0 new messages