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


Groups > comp.lang.python > #70540

Re: how to write list in a file

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <dpalao.python@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'column': 0.07; 'reason,': 0.07; 'subject:file': 0.07; '#print': 0.09; '[];': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'suggest': 0.14; 'block.': 0.16; 'dhananjay': 0.16; 'files:': 0.16; 'points:': 0.16; 'skipped': 0.16; 'trying': 0.19; 'advance.': 0.19; 'everyone,': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; 'skip:e 30': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.25; 'header:In- Reply-To:1': 0.27; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; "skip:' 10": 0.31; 'minor': 0.31; 'anyone': 0.31; 'file': 0.32; 'url:python': 0.33; 'could': 0.34; 'message.': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'doing': 0.36; 'url:org': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'follows:': 0.38; 'files': 0.38; 'sure': 0.39; 'url:mail': 0.40; '2nd': 0.60; 'skip:o 30': 0.61; 'to:addr:gmail.com': 0.65; 'thanking': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=qQAzmDCwVTyR6hIXrqB7fbL3Aw3f2aD2+gyV8vtKptg=; b=wMfVKzl3KVocgUPlpHPSnuGpHVT1Xp0H/iIVg01VSUFv2IxGvHll9XcLIjbyFJr/AP SuCw9H6Oo5r1neWmAu9NGcWFIAcmSIAdVoBoi1S8R7S9ASn2x7Q3PK3oVaG06tQXJLvd tBMjvlXduLs/X6z8f6VZxY7tRJiiHHm+BZO5V+w1QHlbF+jV0x7anmqYIIKJ4ogUS2HL n9qtOvXGA+432hvTPw2czocvCZLfPZoAsWifntO1fPrC9zymIyayN+/jRFc7tZXYFBzs iOJB0o24SCd84mLZqjJWX1Ksu0sJZ31L9D+pQSF6x7TLFnOFjHOaP+dMRW4ZJ+45EjOh MB1g==
MIME-Version 1.0
X-Received by 10.180.218.35 with SMTP id pd3mr817897wic.26.1398242444544; Wed, 23 Apr 2014 01:40:44 -0700 (PDT)
In-Reply-To <CAPxp-0R8dSR8wzcn=cCTnHMJ=qb7Ryocyi11qAM+VVufzCVVzA@mail.gmail.com>
References <CAPxp-0R8dSR8wzcn=cCTnHMJ=qb7Ryocyi11qAM+VVufzCVVzA@mail.gmail.com>
Date Wed, 23 Apr 2014 10:40:44 +0200
Subject Re: how to write list in a file
From David Palao <dpalao.python@gmail.com>
To Dhananjay <dhananjay.c.joshi@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9463.1398242446.18130.python-list@python.org> (permalink)
Lines 66
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398242446 news.xs4all.nl 2936 [2001:888:2000:d::a6]:50444
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70540

Show key headers only | View raw


2014-04-23 9:53 GMT+02:00 Dhananjay <dhananjay.c.joshi@gmail.com>:
> Hello everyone,
>
> I am trying hard to write a list to a file as follows:
>
>
> def average_ELECT(pwd):
>     os.chdir(pwd)
>     files = filter(os.path.isfile, os.listdir('./'))
>     folders = filter(os.path.isdir, os.listdir('./'))
>     eelec = 0.0; evdw = 0.0; EELEC = []; elecutoff = [];
>     g = Gnuplot.Gnuplot()
>     for f1 in files:
> #       if f1[21:23]=='12':
>         if f1[27:29]==sys.argv[1]: # vdw cutoff remains constant; see 2nd
> column of output
>             fl1 = open(f1,'r').readlines()
>     #       print len(fl1)
>
>             for i in range(1, len(fl1)):
>                 fl1[i]=fl1[i].split()
>                 eelec = eelec + float(fl1[i][1])
>                 evdw = evdw + float(fl1[i][2])
>                 #print fl1[i][1], fl1[i][2]
>             avg_eelec = eelec/40
>             avg_evdw = evdw/40
>     #       print eelec, evdw
>     #       print f1[21:23], f1[27:29], avg_eelec, avg_evdw
>             print f1[21:23], f1[27:29], avg_eelec
>     #       EELEC.append(avg_eelec); elecutoff.append(float(f1[21:23]))
>             eelec=0.0; evde=0.0;
>             a = f1[21:23]+' '+f1[27:29]+' '+str(avg_eelec)
>             EELEC.append(a)
>     print sorted(EELEC)
>     with open('EElect_elec12-40_vdwxxx.dat','w') as wr:
>         for i in EELEC:
>             print i
>             wr.write("%s\n" % i)
>     wr.close()
>
>
> The script is printing "print sorted(EELEC)" as well as "print f1[21:23],
> f1[27:29], avg_eelec" very well.
> However, for some reason, I neither see any file (expected to see
> EElect_elec12-40_vdwxxx.dat as per the script) generated nor any error
> message.
>
> Could anyone suggest me correction here.
>
> Thanking you in advance.
>
> -- DJ
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

A couple of minor points:
* You don't need "wr.close()" after the "with open(...) as wr:" block.
* Are you sure that you are looking in the right directory?
* Why do you need the "g = Gnuplot.Gnuplot()" line? It looks to me
that you have skipped some things while doing copy and paste?

Best regards

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


Thread

Re: how to write list in a file David Palao <dpalao.python@gmail.com> - 2014-04-23 10:40 +0200

csiph-web