title: Python average by length of a string
body: <p>I have a data file which has 6 rows of numbers within seven columns of numbers.</p>
  
  <p>Example:</p>
  
  <pre><code>5 8 3 21 10 99 3
  14 20 100 34 94 2 3
  4 2 102 330 101 3 9
  9 22 77 721 30 4 9
  99 3 4 1 0 55 8 4
  8 90 3 1 99 4 101
  </code></pre>
  
  <p>I have the following code and would like to know how to find the average of each line and then each row.</p>
  
  <pre><code>def main():
  
      fileName = input("Enter the name of the data file: ")
      print()
      infile = open(fileName, "r")
  
  #   get data
  
      sum = 0
      count = 0
      data = infile.read().strip().split()
      for line in len(data):
          sum = sum / len(data)
  
      print("This is sum: ", sum) 
  main()
  </code></pre>
  
  <p>I may only open the file once, I must have everything done with loops, which mean that I must do all of the math for the columns and the rows within looks that reside within the first loop. The output should look something like:</p>
  
  <p>There is a chance that upon turn in, my instructor might set the data file to have more than 7 colums and 6 rows. As such, the code must be able to adapt to this change. The output below was derived from another person who had a similar program to make, but used a fixed file formate. That code would not work in the case that there is a new column added like 5, 10, 15, 20, 25, 95, 100... ect or an additional car.</p>
  
  <pre><code>                           Speed (MPH)
  
     Car   :     30     40     50     60     70     80     90  : Average Noise
  
  ----------------------------------------------------------------------------
  
      1    :     88     90     94     98    100    110    120  :    100.0
      2    :     75     77     80     86     94    103    113  :     89.7
      3    :     80     83     85     94    111    111    121  :     97.9
      4    :     68     71     76     85     96    122    125  :     91.9
      5    :     77     84     91    102    105    112    119  :     98.6
      6    :     81     85     90     96    102    109    134  :     99.6
  
  ----------------------------------------------------------------------------
  
   Average :     78.2   81.7   86.0   93.5  101.3  111.2  122.0
  
  
   Maximum :     88     90     94    102    111    122    134
  </code></pre>
  
  <p>Anyone have any idea on how to go about doing this? Everything must be flexable to change based upon the imported data file. Just like an excel sheet is.</p>