Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19843

Re: Help writelines

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nicholas.dokos@hp.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'string.': 0.04; 'python': 0.08; '*you*': 0.09; 'alot': 0.09; 'newline': 0.09; 'none.': 0.09; 'skip:f 30': 0.13; 'cc:addr:python-list': 0.15; '"\\n")': 0.16; 'newlines': 0.16; 'nick': 0.16; 'works...': 0.16; 'wrote:': 0.16; 'subject:Help': 0.17; 'trying': 0.20; '(but': 0.21; 'header:In- Reply-To:1': 0.22; 'figure': 0.23; 'skip:[ 10': 0.27; 'tried': 0.27; 'producing': 0.28; 'elements': 0.28; 'cc:addr:python.org': 0.29; 'object.': 0.30; 'skip:\xc2 20': 0.30; 'strings.': 0.30; 'equivalent': 0.30; 'everyone,': 0.32; 'list': 0.32; 'object': 0.33; 'file.': 0.34; 'calling': 0.34; '...': 0.35; 'file': 0.35; 'cc:2**1': 0.36; 'sequence': 0.36; 'but': 0.37; 'could': 0.37; 'skip:o 20': 0.38; 'e.g.': 0.38; 'help': 0.39; 'subject:: ': 0.39; '8bit%:8': 0.40; 'join': 0.61; 'header:Received:6': 0.61; 'skip:w 30': 0.63; 'reply-to:no real name:2**0': 0.72; 'header:Reply- to:1': 0.84; '8bit%:16': 0.84
To Anatoli Hristov <tolidtm@gmail.com>
From Nick Dokos <nicholas.dokos@hp.com>
Subject Re: Help writelines
In-Reply-To Message from Anatoli Hristov <tolidtm@gmail.com> of "Fri\, 03 Feb 2012 21\:27\:48 +0100." <CAKhY55OeHanpWC=WfzMo1nzujsrVC2oAD89R6So3ZG3mobBphg@mail.gmail.com>
References <CAKhY55OeHanpWC=WfzMo1nzujsrVC2oAD89R6So3ZG3mobBphg@mail.gmail.com>
Organization HPCS
X-Mailer MH-E 8.3.1; nmh 1.3; GNU Emacs 24.0.92
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding quoted-printable
Date Fri, 03 Feb 2012 15:47:37 -0500
Sender nicholas.dokos@hp.com
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To nicholas.dokos@hp.com
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5419.1328302606.27778.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328302606 news.xs4all.nl 6947 [2001:888:2000:d::a6]:40311
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19843

Show key headers only | View raw


Anatoli Hristov <tolidtm@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Help writelines Nick Dokos <nicholas.dokos@hp.com> - 2012-02-03 15:47 -0500

csiph-web