Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Help writelines
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Markus Rother  
View profile  
 More options Feb 3, 3:41 pm
Newsgroups: comp.lang.python
From: Markus Rother <markus.rot...@web.de>
Date: Fri, 03 Feb 2012 21:41:54 +0100
Local: Fri, Feb 3 2012 3:41 pm
Subject: Re: Help writelines
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Dokos  
View profile  
 More options Feb 3, 3:47 pm
Newsgroups: comp.lang.python
From: Nick Dokos <nicholas.do...@hp.com>
Date: Fri, 03 Feb 2012 15:47:37 -0500
Local: Fri, Feb 3 2012 3:47 pm
Subject: Re: Help writelines

 |  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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Feb 3, 3:56 pm
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Fri, 03 Feb 2012 15:56:29 -0500
Local: Fri, Feb 3 2012 3:56 pm
Subject: Re: Help writelines
On 02/03/2012 03:27 PM, Anatoli Hristov wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »