file I/O and arithmetic calculation

Discussion in 'Python' started by Keira Wilson, May 22, 2013.

  1. Keira Wilson

    Keira Wilson Guest

    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
     
    Keira Wilson, May 22, 2013
    #1
    1. Advertisements

  2. 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))*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])
    r=t/10.
    print "%s C1: %f R1: %f"%(m,c,r)

    --
    Denis McMahon,
     
    Denis McMahon, May 23, 2013
    #2
    1. Advertisements

  3. ----------------------------------------
    > From:

    [...]
    >
    > import re
    > def v(s):
    > l=len(s)
    > t=0.
    > for i in range(l):
    > t=t+(abs(ord(s))*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])
    > r=t/10.
    > print "%s C1: %f R1: %f"%(m,c,r)
    >
    > --
    > Denis McMahon,
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Can you send it again without tabs?
     
    Carlos Nepomuceno, May 23, 2013
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    560
    Nilesh Deshpande
    Nov 30, 2005
  2. Micah
    Replies:
    2
    Views:
    390
    Jacek Dziedzic
    Apr 28, 2004
  3. robert maas, see http://tinyurl.com/uh3t

    Apparently inconsistent arithmetic calculation

    robert maas, see http://tinyurl.com/uh3t, Jan 27, 2007, in forum: Perl
    Replies:
    2
    Views:
    2,053
    robert maas, see http://tinyurl.com/uh3t
    Jan 27, 2007
  4. joshc
    Replies:
    5
    Views:
    752
    Keith Thompson
    Mar 31, 2005
  5. inbaly
    Replies:
    1
    Views:
    466
    inbaly
    Feb 2, 2010
  6. inbaly
    Replies:
    1
    Views:
    1,009
    inbaly
    Feb 2, 2010
  7. Ironhide

    checksum calculation for file offsets

    Ironhide, Apr 26, 2010, in forum: Perl Misc
    Replies:
    2
    Views:
    322
    Steve C
    Apr 27, 2010
  8. Keira Wilson

    Re: file I/O and arithmetic calculation

    Keira Wilson, May 23, 2013, in forum: Python
    Replies:
    2
    Views:
    182
    Keira Wilson
    May 24, 2013
Loading...