title: Automatically identifying ultimate and penultimate columns as variable in python
body: <p>I am trying to automize a part of the following code that I introduced <a href="https://stackoverflow.com/questions/31142884/using-first-row-as-variable-with-python">HERE</a>. As it is, the code works and achieves my desired final result. However, one of the variables is semi-automatic and I would like to automize it.</p>
  
  <p>This is the code that I have to transfer raw data into an input file.</p>
  
  <pre><code># -*- coding: UTF-8 -*-
  
  import logging
  from optparse import OptionParser
  import sys
  
  def main():
      LEVELS = {'debug': logging.DEBUG,
                'info': logging.INFO,
                'warning': logging.WARNING,
                'error': logging.ERROR,
                'critical': logging.CRITICAL}
  
      usage = "usage: arff automate [options]\n ."
      parser = OptionParser(usage=usage, version="%prog 1.0")
  
      #Defining options   
      parser.add_option("-l", "--log", dest="level_name", default="info", help="choose the logging level: debug, info, warning, error, critical")    
  
      #Parsing arguments
      (options, args) = parser.parse_args()
  
      #Mandatory arguments    
      if len(args) != 1:
          parser.error("incorrect number of arguments")
  
      inputPath = args[0]
  
  
      # Start program ------------------
  
      with open(inputPath, "r") as f:
          strip = str.strip
          split = str.split
          data = [line.strip().split() for line in f]
  
  
  ###############################################################
  ## Below Here is the part that I need to modify
  ###############################################################
  
  
      numAtts = len(data[0])
      logging.info(" Number of attributes : "+str(numAtts) )
  
      print "@relation 'o_wekaarff.arff'"
  
      for e in range(numAtts):
          if e &lt; 5:
              print "@ATTRIBUTE 'att [{0}]' numeric".format(data[0][e])
          elif e == 5:
              print "@ATTRIBUTE 'att [{0}]' binary".format(data[0][e])
          elif e == 6:
              print "@ATTRIBUTE 'att [{0}]' string".format(data[0][e])
  
  ###############################################################
  ## Above Here is the part that I need to modify
  ###############################################################
  
      classSet = set()
      for e in data:
          classSet.add(e[-1])
      print ""
  
      print "@data"
  
      next(iter(data))
      for item in data[1:]:
          print ",".join(item[0:])
  
  
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>This is the raw data I have used:</p>
  
  <pre><code>F1  F2  F3  F4  F5  {0,1}   STRING
  7209    3004    15302   5203    2   1   EXAMPLEA
  6417    3984    16445   5546    15  1   EXAMPLEB
  8822    3973    23712   7517    18  0   EXPAMPLEC
  </code></pre>
  
  <p>The result obtained is the following:</p>
  
  <pre><code>@relation 'o_wekaarff.arff'
  @ATTRIBUTE 'att [F1]' numeric
  @ATTRIBUTE 'att [F2]' numeric
  @ATTRIBUTE 'att [F3]' numeric
  @ATTRIBUTE 'att [F4]' numeric
  @ATTRIBUTE 'att [F5]' numeric
  @ATTRIBUTE 'att [{0,1}]' binary
  @ATTRIBUTE 'att [STRING]' string
  
  @data
  7209,3004,15302,5203,2,1,EXAMPLEA
  6417,3984,16445,5546,15,1,EXAMPLEB
  8822,3973,23712,7517,18,0,EXPAMPLEC
  </code></pre>
  
  <p>However, this is sample data. In the future, it will be possible that I have thousands of columns. Because of that, I would like the script to automatically recognize what are the ultimate and penultimate columns - in order to not manually define what number that they are in, as it is done currently with <code>e</code>.</p>
  
  <p>Is there a <code>pythonic</code> way to do this, without having to manually define the number of columns that is later printed with a specific variable, as indicated in the output example?</p>