Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > file I/O and arithmetic calculation

Reply
Thread Tools

file I/O and arithmetic calculation

 
 
Keira Wilson
Guest
Posts: n/a
 
      05-22-2013
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

 
Reply With Quote
 
 
 
 
Denis McMahon
Guest
Posts: n/a
 
      05-22-2013
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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Carlos Nepomuceno
Guest
Posts: n/a
 
      05-23-2013
----------------------------------------
> From: (E-Mail Removed)

[...]
>
> 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, (E-Mail Removed)
> --
> http://mail.python.org/mailman/listinfo/python-list


Can you send it again without tabs?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checksum calculation for file offsets Ironhide Perl Misc 2 04-27-2010 06:10 PM
how to do an arithmetic factorization and calculation inbaly Java 1 02-02-2010 07:48 AM
how to do an arithmetic factorization and calculation inbaly ASP .Net 1 02-02-2010 07:47 AM
Apparently inconsistent arithmetic calculation robert maas, see http://tinyurl.com/uh3t Perl 2 01-27-2007 11:57 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM



Advertisments