Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1100695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:55:15+00:00 2026-05-17T00:55:15+00:00

Hi here is my problem. I have a program that calulcates the averages of

  • 0

Hi here is my problem. I have a program that calulcates the averages of data in columns.
Example

Bob
1
2
3

the output is

Bob
2

Some of the data has ‘na’s
So for Joe

Joe
NA
NA
NA

I want this output to be NA

so I wrote an if else loop

The problem is that it doesn’t execute the second part of the loop and just prints out one NA. Any suggestions?

Here is my program:

with open('C://achip.txt', "rtU") as f:
    columns = f.readline().strip().split(" ")
    numRows = 0
    sums = [0] * len(columns)

    numRowsPerColumn = [0] * len(columns) # this figures out the number of columns

    for line in f:
        # Skip empty lines since I was getting that error before
        if not line.strip():
            continue

        values = line.split(" ")
        for i in xrange(len(values)):
            try: # this is the whole strings to math numbers things
                sums[i] += float(values[i])
                numRowsPerColumn[i] += 1
            except ValueError:
                continue 

    with open('c://chipdone.txt', 'w') as ouf:
        for i in xrange(len(columns)):
           if numRowsPerColumn[i] ==0 :
               print 'NA' 
           else:
               print>>ouf, columns[i], sums[i] / numRowsPerColumn[i] # this is the average calculator

The file looks like so:

Joe Bob Sam
1 2 NA
2 4 NA
3 NA NA
1 1  NA

and final output is the names and the averages

Joe Bob Sam 
1.5 1.5 NA

Ok I tried Roger’s suggestion and now I have this error:

Traceback (most recent call last):
File “C:/avy14.py”, line 5, in
for line in f:
ValueError: I/O operation on closed file

Here is this new code:

with open(‘C://achip.txt’, “rtU”) as f:
columns = f.readline().strip().split(” “)
sums = [0] * len(columns)
rows = 0
for line in f:
line = line.strip()
if not line:
continue

rows += 1
for col, v in enumerate(line.split()):
if sums[col] is not None:
if v == “NA”:
sums[col] = None
else:
sums[col] += int(v)

with open(“c:/chipdone.txt”, “w”) as out:
for name, sum in zip(columns, sums):
print >>out, name,
if sum is None:
print >>out, “NA”
else:
print >>out, sum / rows

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:55:16+00:00Added an answer on May 17, 2026 at 12:55 am
    with open("c:/achip.txt", "rU") as f:
      columns = f.readline().strip().split()
      sums = [0.0] * len(columns)
      row_counts = [0] * len(columns)
    
      for line in f:
        line = line.strip()
        if not line:
          continue
    
        for col, v in enumerate(line.split()):
          if v != "NA":
            sums[col] += int(v)
            row_counts[col] += 1
    
    with open("c:/chipdone.txt", "w") as out:
      for name, sum, rows in zip(columns, sums, row_counts):
        print >>out, name,
        if rows == 0:
          print >>out, "NA"
        else:
          print >>out, sum / rows
    

    I’d also use the no-parameter version of split when getting the column names (it allows you to have multiple space separators).

    Regarding your edit to include input/output sample, I kept your original format and my output would be:

    Joe 1.75
    Bob 2.33333333333
    Sam NA
    

    This format is 3 rows of (ColumnName, Avg) columns, but you can change the output if you want, of course. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here is my problem. I have a program that has 11 functions. I
Here is the problem: I have two columns in a table that, for each
Hi I have a program that needs to be fix. Here is the problem:
I have a simple PHP program that list some topics. for example I have
I have a particular problem, I have some program that I cannot modify but
Here's the problem: I have a data-bound list of items, basically a way for
Here's my problem: I have an unmanaged dll that I made.I'm calling one of
Here's the problem: I have a project that needs to be migrated to Microsoft
Here is the situation: I have to create a program that would input another
I have a program that solves the weighted interval scheduling problem using dynamic programming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.