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


Groups > comp.lang.python > #19844

Re: Help writelines

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!87.79.20.101.MISMATCH!newsreader4.netcologne.de!news.netcologne.de!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 <d@davea.name>
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; 'python': 0.08; 'alot': 0.09; 'append': 0.09; 'loop.': 0.09; 'newline': 0.09; 'newline,': 0.09; 'cc:addr:python-list': 0.15; '"\\n")': 0.16; 'linefeed': 0.16; 'received:192.168.1.104': 0.16; 'shorthand': 0.16; 'works...': 0.16; 'wrote:': 0.16; 'subject:Help': 0.17; 'trying': 0.20; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'figure': 0.23; 'end,': 0.23; 'item.': 0.23; 'loop,': 0.23; 'string': 0.24; 'cc:2**0': 0.25; 'pm,': 0.26; 'skip:[ 10': 0.27; 'tried': 0.27; 'cc:addr:python.org': 0.29; 'object.': 0.30; 'everyone,': 0.32; 'does': 0.32; 'list': 0.32; 'thanks': 0.32; 'header:User-Agent:1': 0.33; 'there': 0.33; 'loop': 0.34; 'puts': 0.34; 'things': 0.35; 'file': 0.35; 'vote': 0.36; 'skip:" 10': 0.36; 'but': 0.37; 'could': 0.37; 'received:192': 0.38; 'help': 0.39; 'received:192.168.1': 0.39; 'subject:: ': 0.39; 'change': 0.40; 'join': 0.61; 'your': 0.61; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'choices.': 0.84; 'items,': 0.91
Date Fri, 03 Feb 2012 15:56:29 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Thunderbird/3.1.16
MIME-Version 1.0
To Anatoli Hristov <tolidtm@gmail.com>
Subject Re: Help writelines
References <CAKhY55OeHanpWC=WfzMo1nzujsrVC2oAD89R6So3ZG3mobBphg@mail.gmail.com>
In-Reply-To <CAKhY55OeHanpWC=WfzMo1nzujsrVC2oAD89R6So3ZG3mobBphg@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:gdznInsBTA49zMjfVk+VlmLi/EesbIlQyqBtxCH9wUD RagI7+13SK0Ui3wZmZSHCsxtvF8E5UwjdIAANQxTuPByUyRF8f He08fyGegibgLBH196lVyrONqIP+KAHDPrdIZ653DJpamhK/Pt xo08NiIktmufEUm+df/VJeTwQpLrRPj7XzBxZxEsk12Kjp9idX ZGLpGnOMCNhDtpaPAK+0U6/vZU6R+cbIVvcprnjfzmjZk6HkyT OF/g9xxc1JnVlLadJwXUX7S9XSGyj+sk3zDVtnT4ar906ELc47 877q0R9iP5c/vT3Ud8/5e3XVk2GCw5nYp+yY9fmd0Mu3leJuQ= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To d@davea.name
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.5420.1328302625.27778.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328302625 news.xs4all.nl 6976 [2001:888:2000:d::a6]:40521
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19844

Show key headers only | View raw


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

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


Thread

Re: Help writelines Dave Angel <d@davea.name> - 2012-02-03 15:56 -0500

csiph-web