title: Extract and sort data from .mdb file using mdbtools in Python
body: <p>I'm quite new to Python, so any help will be appreciated. I am trying to extract and sort data from 2000 .mdb files using <code>mdbtools</code> on Linux. So far I was able to just take the .mdb file and dump all the tables into .csv. It creates huge mess since there are lots of files that need to be processed. </p>
  
  <p>What I need is to extract particular sorted data from particular table. Like for example, I need the table called "Voltage". The table consists of numerous cycles and each cycle has several rows also. The cycles usually go in chronological order, but in some cases time stamp get recorded with delay. Like cycle's one first row can have later time than cycles 1 first row. I need to extract the latest row of the cycle based on time for the first or last five cycles. For example, in table below, I will need the second row.</p>
  
  <pre><code>Cycle#    Time        Data
    1      100.59        34
    1      101.34        54
    1      98.78         45  
    2      
    2
    2   ........... 
  </code></pre>
  
  <p>Here is the script I use. I am using the command <code>python extract.py  table_files.mdb.</code> But I would like the script to just be invoked with ./extract.py. The path to filenames should be in the script itself.</p>
  
  <pre><code>import sys, subprocess, os
  
  DATABASE = sys.argv[1]
  
  subprocess.call(["mdb-schema", DATABASE, "mysql"])
  
  # Get the list of table names with "mdb-tables"
  table_names = subprocess.Popen(["mdb-tables", "-1", DATABASE],
                                 stdout=subprocess.PIPE).communicate()[0]
  tables = table_names.splitlines()
  
  print "BEGIN;" # start a transaction, speeds things up when importing
  sys.stdout.flush()
  
  # Dump each table as a CSV file using "mdb-export",
  # converting " " in table names to "_" for the CSV filenames.
  for table in tables:
      if table != '':
          filename = table.replace(" ","_") + ".csv"
          file = open(filename, 'w')
          print("Dumping " + table)
          contents = subprocess.Popen(["mdb-export", DATABASE, table],
                                      stdout=subprocess.PIPE).communicate()[0]
          file.write(contents)
          file.close()
  </code></pre>