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


Groups > comp.lang.python > #45741 > unrolled thread

file I/O and arithmetic calculation

Started byKeira Wilson <wilkeira@gmail.com>
First post2013-05-23 01:13 +0900
Last post2013-05-23 03:02 +0300
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  file I/O and arithmetic calculation Keira Wilson <wilkeira@gmail.com> - 2013-05-23 01:13 +0900
    Re: file I/O and arithmetic calculation Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-22 23:54 +0000
      RE: file I/O and arithmetic calculation Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-23 03:02 +0300

#45741 — file I/O and arithmetic calculation

FromKeira Wilson <wilkeira@gmail.com>
Date2013-05-23 01:13 +0900
Subjectfile I/O and arithmetic calculation
Message-ID<mailman.1970.1369239228.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Dear all,

I would appreciate if someone could write a simple python code for the
purpose below:

I have five text files each of 10 columns by 10 rows as follows:

file_one = 'C:/test/1.txt'
file_two = 'C:/test/2.txt' . . .
file_five = 'C:/test/5.txt'

 I want to calculate the mean of first row (10 elements) for each file (5
files), if mean of first column (10 elements) of each file (5 files) is 50.

Thank you in advance.

Keira

[toc] | [next] | [standalone]


#45760

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-05-22 23:54 +0000
Message-ID<knjlr4$v6m$6@dont-email.me>
In reply to#45741
On Thu, 23 May 2013 01:13:19 +0900, Keira Wilson wrote:

> I would appreciate if someone could write a simple python code for the
> purpose below:

Didn't have your data, so couldn't verify it completely, but try this:

import re
def v(s):
 l=len(s)
 t=0.
 for i in range(l):
  t=t+(abs(ord(s[i]))*1.)
 return t/(l*1.)
for n in range(5):
 m="c:/test/"+str(n+1)+".txt"
 f=open(m,"r")
 d=[]
 t=0.
 for l in range(10):
  d=d+[re.findall(r"[0-9.eE+-]+",f.readline())]
  t=t+v(d[l][0])
 f.close()
 c=t/10.
 if c==50.:
  t=0.
  for u in range(10):
   t=t+v(d[0][u])
  r=t/10.
  print "%s C1: %f R1: %f"%(m,c,r)

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#45761

FromCarlos Nepomuceno <carlosnepomuceno@outlook.com>
Date2013-05-23 03:02 +0300
Message-ID<mailman.1982.1369267337.3114.python-list@python.org>
In reply to#45760
----------------------------------------
> From: denismfmcmahon@gmail.com
[...]
>
> import re
> def v(s):
> l=len(s)
> t=0.
> for i in range(l):
> t=t+(abs(ord(s[i]))*1.)
> return t/(l*1.)
> for n in range(5):
> m="c:/test/"+str(n+1)+".txt"
> f=open(m,"r")
> d=[]
> t=0.
> for l in range(10):
> d=d+[re.findall(r"[0-9.eE+-]+",f.readline())]
> t=t+v(d[l][0])
> f.close()
> c=t/10.
> if c==50.:
> t=0.
> for u in range(10):
> t=t+v(d[0][u])
> r=t/10.
> print "%s C1: %f R1: %f"%(m,c,r)
>
> --
> Denis McMahon, denismfmcmahon@gmail.com
> --
> http://mail.python.org/mailman/listinfo/python-list

Can you send it again without tabs? 		 	   		  

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web