title: Python - getting average from last 5 digits float objects
body: <p>Reads file Qdata.txt and calculates the average of the values in 
    the 2nd column (or the 3rd column indeed, if date is considered as the 1st column</p>
  
  <pre><code>import sys
  
  td = open("Qdata.txt", "r")  # opening the file to variable ("file handle") td
  
  sum = 0
  n = 0
  firstround = True
  
  for line in td:
      if (firstround):
          firstround = False  # nothing else is done for the first line (header)
      else:
          fields = line.split()  # This creates a list containing the strings on 
          # the line, by default separated by spaces or tabs.
          # Now fields[0] contains the date, fields[1] the 
          # 1st data value and fields[2] the 2nd one.
          try:
              sum = sum + float(fields[1])  # increasing the cumulative value
              field1 = (fields[1])
  
              print(field1)
  
              n = 5
          # Handling possible errors.
          except IndexError:  # IndexError occurs e.g. in the case of empty lines
              # (when fields[2], for example, doesn't exist)
              continue
          except ValueError:  # ValueError occurs e.g. if there are letters instead of 
              # numbers (when conversion to float causes an error)
              print("Incorrect values in the file.")
              sys.exit()
  print("Average over the whole period was ", sum / n)
  print("Total number of values was ", n)
  </code></pre>
  
  <p>This is the Qdata.txt</p>
  
  <pre><code>Date   3700300   6701500
  20000101 21.00   223.00  
  20000102 20.00   218.00  
  20000103 18.00   218.00  
  20000104 17.00   213.00  
  20000105 17.00   210.00  
  20000106 18.00   210.00  
  20000107 21.00   210.00  
  20000108 23.00   208.00  
  20000109 27.00   201.00  
  20000110 28.00   199.00  
  20000111 26.00   196.00  
  20000112 24.00   196.00  
  20000113 23.00   194.00  
  20000114 21.00   192.00  
  20000115 19.00   185.00  
  20000116 17.00   183.00  
  20000117 12.00   179.00  
  20000118 11.00   173.00  
  20000119 10.00   171.00  
  20000120 9.80   167.00  
  20000121 9.00   165.00  
  20000122 8.40   163.00  
  20000123 7.50   157.00  
  20000124 7.10   156.00  
  20000125 6.70   150.00  
  20000126 6.40   148.00  
  20000127 6.00   148.00  
  20000128 5.90   147.00  
  20000129 5.50   145.00  
  20000130 5.40   143.00  
  20000131 5.30   140.00  
  20000201 5.30   140.00  
  </code></pre>
  
  <p>So, now I want the sum and later avg with numbers from field1. If I try to extract from field1[-5:], it does not work. What way can I get the last 5 digits of the middle column to sum and avg, I was thinking to make a list?</p>