Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.036 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.01; 'skip:[ 20': 0.04; 'column': 0.07; 'subject:file': 0.07; 'filenames': 0.09; '###': 0.16; '6th': 0.16; 'csv': 0.16; 'separated': 0.16; 'subject:arithmetic': 0.16; "skip:' 30": 0.19; 'to:name:python- list@python.org': 0.22; 'print': 0.22; 'received:65.55.116': 0.24; 'skip:{ 20': 0.24; 'subject:/': 0.26; 'values': 0.27; 'header:In- Reply-To:1': 0.27; 'lines': 0.31; "skip:' 10": 0.31; 'piece': 0.31; 'file': 0.32; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; '8bit%:6': 0.40; '5th': 0.60; 'such': 0.63; '1st': 0.74; 'as:': 0.81; '8bit%:24': 0.84; 'improved.': 0.84; '8bit%:18': 0.93; 'average': 0.93 X-TMN: [cY9LPUhQwKYVSVPgMI2HoszvcaLn36Ki] X-Originating-Email: [carlosnepomuceno@outlook.com] From: Carlos Nepomuceno To: "python-list@python.org" Subject: RE: file I/O and arithmetic calculation Date: Thu, 23 May 2013 06:15:58 +0300 Importance: Normal In-Reply-To: References: , , , , , Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 23 May 2013 03:15:58.0818 (UTC) FILETIME=[D8A1BC20:01CE5763] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369278966 news.xs4all.nl 15929 [2001:888:2000:d::a6]:50646 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45769 The last line of my noob piece can be improved. So this is it:=0A= =0A= ### 1strow_average.py ###=0A= #Assuming you have CSV (comma separated values) files such as:=0A= #1.txt =3D '0=2C1=2C2=2C3=2C4=2C5=2C6=2C7=2C8=2C9\n' \=0A= #=A0=A0=A0=A0=A0=A0=A0 '10=2C11=2C12=2C13=2C14=2C15=2C16=2C17=2C18=2C19\n' = \=0A= #=A0=A0=A0=A0=A0=A0=A0 '20=2C21=2C22=2C23=2C24=2C25=2C26=2C27=2C28=2C29\n' = ...=0A= #=0A= # Usage: contents[file][row][column]=0A= # contents[0]=A0=A0=A0=A0=A0=A0 : file '1.txt'=0A= # contents[1][2]=A0=A0=A0 : 3rd row of file '2.txt'=0A= # contents[3][4][5] : value on the 6th column of 5th row of file '4.txt'=0A= # len(contents)=A0=A0=A0=A0 : quantity of files=0A= # len(contents[4])=A0 : quantity of lines in file '5.txt'=0A= # len(contents[4][0]: quantity of values in the 1st line of file '5.txt'=0A= =0A= filenames =3D ['1.txt'=2C '2.txt'=2C '3.txt'=2C '4.txt'=2C '5.txt']=0A= contents=A0 =3D [[[int(z) for z in y.split('=2C')] for y in open(x).read().= split()] for x in filenames]=0A= s1c=A0 =3D [sum([r[0] for r in f]) for f in contents]=0A= a1r=A0 =3D [sum(f[0])/float(len(f[0])) for f in contents]=0A= print '\n'.join(['File "{}" has 1st row average =3D {:.2f}'.format(n=2Ca1r[= i]) for i=2Cn in enumerate(filenames) if s1c[i]=3D=3D50]) =